Giter VIP home page Giter VIP logo

Comments (10)

moses-palmer avatar moses-palmer commented on July 20, 2024 3

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.

moses-palmer avatar moses-palmer commented on July 20, 2024

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.

raseribanez avatar raseribanez commented on July 20, 2024

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.

spidy1996 avatar spidy1996 commented on July 20, 2024

it says Key Error: 'REMOTE_DISPLAY'

from pynput.

jangid avatar jangid commented on July 20, 2024

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.

moses-palmer avatar moses-palmer commented on July 20, 2024

In that case, pynput will not work; it is an X client. See #184 for a possible future solution.

from pynput.

Remi-EA avatar Remi-EA commented on July 20, 2024

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.

ollipal avatar ollipal commented on July 20, 2024

@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.

kasrakazemi avatar kasrakazemi commented on July 20, 2024

@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.

ollipal avatar ollipal commented on July 20, 2024

(@kasrakazemi I replied here)

from pynput.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.