jveitch
Posts: 8
Joined: Sat Dec 17, 2016 1:25 am

Input from client keyboard over VNC

Fri Jan 20, 2017 9:10 am

Hello everyone,
I posted this on the Beginner side of the forum but didn't get much help so am posting it here too.
Problem:
I have written a program in Python2 on my RPi and it runs happily.
The program takes a keystroke from the connected keyboard and turns on/off/speeds up a DC motor.

When I try to submit a keystroke from my MacBook Air via VNC the program does not accept the keystroke as an input to the program it just prints the keystroke.

I have VNC running and it seems to do everything it should be doing. I can connect to the RPi using SSH from my Mac. All connections seem fine, but I can not remotely "input" a keystroke.

What to do???
Thank you for any help.
It's hot here in Australia, hope it's nice where you are.
JV

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: Input from client keyboard over VNC

Fri Jan 20, 2017 10:46 am

Does it work when you ssh in?

mattmiller
Posts: 2059
Joined: Thu Feb 05, 2015 11:25 pm

Re: Input from client keyboard over VNC

Fri Jan 20, 2017 2:01 pm

I posted this on the Beginner side of the forum but didn't get much help so am posting it here too.
em - i asked you to post your code to provide us with more info and you didn't do it - so that's why probably why you got no more replies. :)

texy
Forum Moderator
Forum Moderator
Posts: 5156
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Input from client keyboard over VNC

Fri Jan 20, 2017 2:22 pm

jveitch wrote:Hello everyone,
I posted this on the Beginner side of the forum but didn't get much help so am posting it here too.
Just raise a report for a moderator to move the thread to another forum if you feel it was initial placed incorrectly.
Please don't duplicate posts.

Thanks.
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

jveitch
Posts: 8
Joined: Sat Dec 17, 2016 1:25 am

Re: Input from client keyboard over VNC

Fri Jan 20, 2017 11:24 pm

I apologize for the duplicate posting...didn't know that was an evil thing to do...sorry. I promise to never do it again.
So.....
1. Does my program work with SSH? I do not know, I do not think so. I've just started programming and I got my Python program to work by accepting a keyboard input from the keyboard attached to the RPi by a USB cable. I can SSH from my MacBook Terminal program to the RPi terminal window and give commands to the RPi as if I were sitting at my RPi keyboard using the RPi terminal. But I do not know how to make an entry in Terminal cause anything to happen in a Python program. Is that possible?

2. Posting code here so you can see what the problem is. Well this is a problem. I suppose an experienced programmer would know exactly which snippets of code to post, I do not. Being new to programming I've found it interesting and useful that one program pulls in functions (imports?) from other programs that in turn import things from other programs. As long as they all reside in the same directory things seem to work. One challenge, for me, has been to painstakingly trace back through several external programs to find what every little important bit is. I don't think it would be practical to post 3 or 4 long programs in this little window...or is that how things are done? I don't want to break any more rules.

3. I probably should have explained what my program's objective is. I want to control two small DC motors on a little robot (forward, reverse, speed, stop). I want to control the motors via WiFi.
I have accomplished:
a. wifi connection
b. static ip address
c. SSH connection
d. VNC connection
e. A Python2 program that takes keyboard key inputs (from a keyboard attached to the RPi) to manipulate the two DC motors.
I would like to:
aa. Control the two DC motors from a remote keyboard via WiFi.
(Surely someone must have done this before, it seems so rudimentary.)

Best wishes to all, JV

mattmiller
Posts: 2059
Joined: Thu Feb 05, 2015 11:25 pm

Re: Input from client keyboard over VNC

Sat Jan 21, 2017 1:24 pm

don't think it would be practical to post 3 or 4 long programs in this little window
The code that deals with reading the keyboard would probably be sufficient

User avatar
DougieLawson
Posts: 35161
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Input from client keyboard over VNC

Sat Jan 21, 2017 2:48 pm

jveitch wrote:I don't think it would be practical to post 3 or 4 long programs in this little window...
Post them on a public sharing site like pastebin.com or github gist or equivalent. Post the link on here.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

SomeGuyJim
Posts: 5
Joined: Mon Mar 11, 2019 8:19 pm

Re: Input from client keyboard over VNC

Mon Apr 22, 2019 7:37 pm

Strangely, I've encountered this same problem. I wired up a small robot to respond to key commands via the pynput library. All was fine during bench tests, but when I disconnected and used VNC to remotely send the keyboard commands, the keys were detected over and over again for every press.

I even tried setting booleans to screen out the duplicate key presses. But no luck. So it looks like VNC's keyboard input aren't sufficient to remote control via key strokes. VNC is causing (in technical terms) some keyboard event "weirdness".

But I was able to sidestep this problem by using a socket connection instead. The client code runs pynput and detects the keystrokes. Then it sends a short command to the Raspberry Pi via socket. The Pi acts as the "server" to catch the requests and move the motors.

WARNING: this approach is fine for tinkering just to get some motors moving. But it's fairly unsecure. Also, the latency is pretty bad. This approach will never be as responsive as an RC car or anything.

Here's the (hastily constructed ) code for anyone that googles this later. It has a bug and doesn't handle multiple key presses very well.

Client Code:

Code: Select all

import socket
from pynput import keyboard
import time

try: 
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
	print "Socket successfully created"
except socket.error as err: 
	print "socket creation failed with error %s" %(err) 

port = 65432

try: 
    host_ip = 'YOUR INTERNAL IP HERE'
except socket.gaierror: 
	print "there was an error resolving the host"

# connecting to the server 
s.connect((host_ip, port)) 

print "connected to pi on port == %s" %(host_ip) 
print("Windows client is ready!\r\n")

currentKey = set()

def on_press(key):
    if key == keyboard.Key.up:
        if not bool(currentKey):
            currentKey.add(key)
            #print('Up is held down')
            s.sendall(b'f')
    if key == keyboard.Key.down:
        if not bool(currentKey):
            currentKey.add(key)
            #print('Down is held down')
            s.sendall(b'b')
    if key == keyboard.Key.left:
        if not bool(currentKey):
            currentKey.add(key)
            #print('Left is held down')
            s.sendall(b'l')         
    if key == keyboard.Key.right:
        if not bool(currentKey):
            currentKey.add(key)
            #print('Right is held down')
            s.sendall(b'r')     
    if key == keyboard.Key.space:
        if not bool(currentKey):
            currentKey.add(key)
            #print('space is held down')
            s.sendall(b'space')
           
def on_release(key):
    if(bool(currentKey)):
        s.sendall(b'x')
        currentKey.remove(key)
        #print('key was released')
    if key == keyboard.Key.esc:
        #Stop listener
        return False

while True:
    with keyboard.Listener(
            on_press=on_press,
            on_release=on_release) as listener:
        listener.join()
Good luck!

Return to “Advanced users”