Comments (10)
On Linux, pynput uses X to control the keyboard.
When creating an X connection, the target display/session is specified with the $DISPLAY
environment variable. When you run your script directly from the Pi, it will be set correctly since the terminal was launched from an X session, but when connecting through SSH it will not.
Get the value of $DISPLAY
when you run the script locally on the Pi, and set it when running the script remotely:
$ ssh pi.local
$ DISPLAY=":0" python my-script.py
If you intend to pass keystrokes from your laptop to the Pi, please note the following:
- You will only be able to control an X session; you cannot emulate actual hardware presses. If you intent to control an application running in an X session, this is not a problem.
- Configuring X does not appear to be trivial. You will need to connect remotely to X.
A simple, untested, script to pass keystrokes is:
import os
import queue
from pynput import keyboard
q = queue.Queue()
with keyboard.Listener(
on_press=lambda k: q.put_nowait((k, True)),
on_release=lambda k: q.put_nowait((k, False))) as listener:
os.environ['DISPLAY'] = os.environ['REMOTE_DISPLAY']
controller = keyboard.Controller()
while True:
controller.touch(*q.get())
from pynput.
If you decide to try, you may want to use this branch. The master branch will cause infinite feedback in the event loop.
from pynput.
Hi, I am having the same issue. So I have created a remote SSH connection via putty to my Raspberry Pi. I can do this from both my Linux and Windows systems. I have a very simple Keylog script written in Python, and it runs smoothly, it saves keystrokes into a log file. I can't deploy this through the SSH connection though. The above example differs to mine because I want to save the output in a separate file.
Currently, when I run the script from the SSH environment, I get the same bad-display error. I thought the problem was to do with there being 2 keyboards to potentially log (both ends of the SSH connection) and this was causing problems. But I am still learning.
Is there a way to run this script on my Pi (Raspbian) from the SSH environment (Putty) and save my output into a log or text file?
BTW This is all above board, the raspberry Pi and computers are my own and I connect using my own username and password etc. I am studying computing and IT in Uni - first year, and just messing around with an activity we are doing.
from pynput.
it says Key Error: 'REMOTE_DISPLAY'
from pynput.
What if my linux-box doesn't have X installed? I have a debian machine without GUI. And I have encountered the same issue 'Bad display name ""'.
from pynput.
In that case, pynput will not work; it is an X client. See #184 for a possible future solution.
from pynput.
Hi,
After running via ssh on an embedded computer (on linux) my code who uses pynput package I get the error
Xlib.error.DisplayNameError: Bad display name ""
So I tried $ DISPLAY=":0" python my-script.py
because I checked locally echo $DISPLAY
and got : :0
But then I got this error : Xlib.error.DisplayConnectionError: Can't connect to display ":0": b'No protocol specified\n'
I want to use pynput's keypress detection on the embedded computer who only has a keyboard connected, while running the script on my laptop via ssh, is it possible ?
Thanks
from pynput.
@spidy1996 @Remi-EA @raseribanez @jangid @wroscoe
After trying to find solution, I decided to write a simple library around this spesific issue: sshkeyboard
It allows you to react to user input even through SSH, and it is perfect for example for driving a RC car on Raspberry Pi.
It is many other ways inferior solution to pynput, but it does this one and only thing pretty well.
from pynput.
@spidy1996 @Remi-EA @raseribanez @jangid @wroscoe After trying to find solution, I decided to write a simple library around this spesific issue: sshkeyboard
It allows you to react to user input even through SSH, and it is perfect for example for driving a RC car on Raspberry Pi.
It is many other ways inferior solution to pynput, but it does this one and only thing pretty well.
Hi @ollipal
i tried to run
from sshkeyboard import listen_keyboard
def press(key):
print(f"'{key}' pressed")
def release(key):
print(f"'{key}' released")
listen_keyboard(
on_press=press,
on_release=release,
)
but continually face with this error:
RuntimeError: asyncio.run() cannot be called from a running event loop
is there any solution for this?
from pynput.
(@kasrakazemi I replied here)
from pynput.
Related Issues (20)
- Which python version is the latest release of pynput compatible with?
- Keyboardtracking CTRL + "second_key" doesn't output "second_key"
- pynput not detecting media keys on kde plasma wayland HOT 1
- Type method crashes on cyrillic characters HOT 1
- Suppress mouse move not working when using parsec HOT 1
- mouse listener example for win32_event_filter HOT 1
- Script Continues Rapid Clicking Despite Mouse Button Release (is_pressed Functionality Issue) HOT 6
- Press cmd_r and release with cmd pressed will produce two presses instead of press + release HOT 3
- Can this be used in Micropython? HOT 5
- Get the original pressed key HOT 1
- The Numpad numbers does not work in HotKeys.
- pynput types ">" instead of "|" HOT 1
- Need to sync keyboard lock keys
- AttributeError: record_create_context HOT 5
- capslock crshed on macOS HOT 2
- lazy imports cause a KeyError HOT 4
- Cannot install on linux Ubuntu (depends on pywin32) HOT 2
- name 'keyboard' is not defined HOT 1
- Additional keys can still trigger events HOT 2
- Missing 1.7.7 tarball on PyPI
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pynput.