I wrote a simple python script that gives control over the cursor to a joystick. My way to find out how this works is documented here. Now that works flawlessly but, as soon as I start the script to use the joystick, the mouse is useless, because my python routine sets the value back to its original, whenever a new joystick event comes in.
Thus I want my joystick events to be ignored as long as a key of the keyboard is pressed. I came across the pygame.key.get_pressed() method but this seems to work only, if the pygame window is in focus. I want this script running in background. Should I start using non-pygame events to listen to the keyboard or are there ways to keep track of the keyboard events analogue to the joystick events, which are recognized in background, via pygame?
I expect pygame sets up its own "sandbox" so that it's hard to detect input from outside its window. Your previous question indicates that you are also using the win32api module. We can use that to detect global key presses.
The correct way to detect key presses at the global scope is to set up a keyboard hook using SetWindowsHookEx. Unfortunately, win32api does not expose that method, so we'll have to use a less efficient method.
The GetKeyState method can be used to determine whether a key is down or up. You can continuously check the state of a key to see if the user has pressed or released it lately.
import win32api
import time
def keyWasUnPressed():
print "enabling joystick..."
#enable joystick here
def keyWasPressed():
print "disabling joystick..."
#disable joystick here
def isKeyPressed(key):
#"if the high-order bit is 1, the key is down; otherwise, it is up."
return (win32api.GetKeyState(key) & (1 << 7)) != 0
key = ord('A')
wasKeyPressedTheLastTimeWeChecked = False
while True:
keyIsPressed = isKeyPressed(key)
if keyIsPressed and not wasKeyPressedTheLastTimeWeChecked:
keyWasPressed()
if not keyIsPressed and wasKeyPressedTheLastTimeWeChecked:
keyWasUnPressed()
wasKeyPressedTheLastTimeWeChecked = keyIsPressed
time.sleep(0.01)
Warning: as with any "while True sleep and then check" loop, this method may use more CPU cycles than the equivalent "set a callback and wait" method. You can extend the length of the sleep period to ameliorate this, but the key detection will take longer. For example, if you sleep for a full second, it may take up to one second between when you press a key and when the joystick is disabled.
when your window gains or looses focus you get an ACTIVEEVENT. It's gain and state attributes tell you which state you've gained or lost. The easisest solution would probably be to catch this events in your main event loop and use them to keep track weather you have focus or not. Then you can just ignore joystick events if you don't have the focus.
Related
My use case
I need to know when a (specific) key is pressed and held down. The use case after detection is fairly easy. When the key is released, send a signal to stop the callback (which I know already).
Desired behavior
Here is a rough scheme of how the algo looks like:
def the_callback():
if key_held == the_hotkey:
someObj.start() # this class Obj works totally well so no issues here on
elif key_released == the_hotkey:
someObj.stop()
else:
# we don't care. continue looking for Keyboard events
# here any kinda listener or just a loop which passes events to the callback
I should mention that any kinda listener which blocks the execution is okay as it will run in its own thread (already running pynput.keyboard.Listener in a thread so not a problem)
What I've tried
I used pynput and its pynput.keyboard.Listener to detect key-presses and invoke callbacks accordingly but I couldn't make that work to detect when a key is HELD down.
the current solution looks roughly like:
# not real code. just rough scheme
def on_pressed(key):
if key == my_hotkey:
if running_already: # this part works well already
obj.stop()
else:
obj.start()
else:
# we don't care
with pynput.keyboard.Listener(on_press=on_pressed) as listener:
listener.join() # blocking call until SystemExit, `return False` from callback or `listener.stop()`
I have a very strong feeling that I can make this work by adding on_release=another_callback_that_handles_releases (available within pynput.keyboard.listener).
Perhaps by storing the last known pressed keystroke, and checking if the key released was the same as the hotkey which was pressed earlier but I'm not sure how would I go about it and can that even work?
Then I decided to give keyboard (different lib) a go.
I wrote the below code for the same which can detect keys being held down. This below code achieves almost nearly what I want:
import keyboard as kb, time
while 1:
while kb.is_pressed('q'):
print('Key is held')
time.sleep(0.5) # sleep added just to stop it from spamming the stdout
else:
print('No it\'s Not')
time.sleep(0.5)
The issue with this solution is, it's not very well suited for OSX and Ubuntu. And it has some issues working with special keys. Moreover, I have the hotkey stored as pynput.keyboard.Key.f7 (for eg) or pynput.keyboard.KeyCode(char='s') # for character keys and these enums have different values than what keyboard uses to scan key IDs (using keyboard.hook()).
The final question
How should I go about detecting a key being HELD down. I'd prefer to achieve this using pynput as the rest of the code base uses it but 'keyboard is fine too.
Again I have a feeling that using on_press=a_callback and on_release=another_callback this might be achieved but I'm not entirely sure about it. Lastly, the solution is preferred to be cross platform (I'm fine with using three different functions depending on value of platform.system()).
How would you go about achieving it?
EDIT-1
HERE is what I wrote as an attempt (and MCVE) after suggestion by Isak. This works almost perfectly with just 1 flaw. And that is that it doesn't listen to keypresses right from the program start.
It takes some time for some unknown reason before it starts to actually detect any keypress. Good thing is that once it detects the keypress for the first time, it works flawlessly.
What am I missing there?
Try to check for the key_pressed event on the specific key until the event becomes key_released. So when you detect a click on the key you execute your code and when it detects the release of that key the code stops
I figured out why My Approach was taking a lot of time to initialize before starting the Listener. It was because of the while loop which didn't have any time.sleep() calls and it was probably messing with the system (although I wouldn't expect that to happen as it runs in its own thread but probably the while loop doesn't release the GIL as it's just in the loop doing literally nothing without any sort of delay).
I just added time.sleep(0.2) inside the while loop (the outer one). Any delay would do as that would release the GIL for some time and the Listener thread would be processed and made active.
Edit: Accepting the answer by Isak as accepted as that is the correct approach.
I have a python program that uses evdev to simulate gamepad button presses. The device used is "cloned" from an xbox usb gamepad, by using ui = evdev.UInput.from_device(...)
But for some reason it seems that the presses is sometimes inconsistent.
Below is the function i use to simulate an A button tap:
def press_A_button():
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.BTN_A, 1)
ui.write(evdev.ecodes.EV_SYN, 0, 0)
sleep(0.01)
ui.write(evdev.ecodes.EV_KEY, evdev.ecodes.BTN_A, 0)
ui.write(evdev.ecodes.EV_SYN, 0, 0)
sleep(0.01)
If i remove the sleep's altogether, it seems no presses will register at all. Albeit 0.01 seems to be too rapid as well, causing it to appear to not register/skip a tap every now and then. (i simply use jstest-gtk to watch how it behaves)
Here's how i test the function:
while True:
try:
press_A_button()
# putting a sleep here, e.g a sleep(2), seemingly still doesn't
# keep it from sometimes missing the tap
except KeyboardInterrupt:
ui.close()
break
So is there some way to figure out the optimal timing that should be used for it to work consistently?
Or could the problem perhaps not be with my code, but the polling rate of the other software (in this case jstest-gtk) ?
A possibly related issue:
Simulating controller dpad button being held down with Python evdev
I am using Tkinter in python 3 to make a primitive game (I am aware of pyGame). My function that I have bound to KeyReleased is executed when any key is pressed. It seems to work just as KeyPress. Code down below
master.bind("KeyRelease",keyReleased) (with <> on the sides of KeyRelease
Most keyboards and OS's will send a steady stream of press/release events when you hold a key down. If you bind to <KeyRelease>, it absolutely will only fire on key release but your app may be betting multiple key release events, making it appear that they are happening on a press.
Is it possible to cleanly detect a key being held down in (ideally native) Python (2)? I'm currently using Tkinter to handle Keyboard events, but what I'm seeing is that when I'm holding a key down, Key, KeyPress, and KeyRelease events are all firing constantly, instead of the expected KeyPress once and KeyRelease at the end. I've thought about using the timing between events to try to differentiate between repeated firing and the actual event, but the timing seems inconsistent - thus, while doable, it seems like a pain.
Along the same lines, is there a nice way to detect multiple key presses (and all being held down?) I'd like to have just used KeyPress and KeyRelease to detect the start / end of keys being pressed, but that doesn't seem to be working.
Any advice is appreciated.
Thanks!
Use a keyup and keydown handler with a global array:
keys = []
def down(event):
global keys
if not event.keycode in keys:
keys.append(event.keycode)
def up(event):
global keys
keys.remove(event.keycode)
root.bind('<KeyPress>', down)
root.bind('<KeyRelease>', up)
Now you can check for multiple entries in keys. To remove that continuous behavior you described, you have to compare the previous state of keys after an event happens.
I am trying to find some sort of library or function so I can get fast keyboard input.
Right now, using the Conio.h input method, you can hold down a key, but you have to wait a half a second for it to start repeating, the same as in any text box. This seems to be dictated by the cursor repeat delay, shown here.
Any way to get realtime keyboard input rather than having to suffer this small delay?
I've heard of pyHook but that doesn't work for Python 3(.2). Thanks!
You'll need to do it the hard way, creating your own window and then listening for keydown and keyup events, using a timer to trigger the "repeat" of the keypress.
I eventually wrote a small DLL to use the Win32 function GetAsyncKeyState.