I am currently coding a program, with the library 'keyboard' in python 3 on Debian.
So if i hold down a Key. The keyboard-library gets multiple KeyDownEvents instead of one at KeyPress but I only want the first event. Is there anyway to suppress the auto-repeat?
I have already tried multiple terminal commands in python which should stop the keyboard auto-repeat but that mostly only works for printable Chars but I also need only 1 event if for example shift is pressed
My conclusion is that the terminal commands don't effect the keyboard library at all.
Any ideas?
Best regards
Related
I am writing an automation test for a hotkey application and one of the challenges is to find out if the correct key/s are pressed after remapping a key. For example, if I rebind the Q key to A, the letter A should type in when I press Q in the keyboard.
Now, I want to be able to perform this action through a script without having to manually press the rebinded key. I have tried the libraries pywinauto, keyboard, and pynput to no avail; they just type in the exact character/s and no conversion occurs.
How to reproduce:
rebind Q key to A using any hotkey software (using any remapping/hotkey tool e.g. autohotkey, razer synapse, logitech setpoint, microsoft mouse and keyboard center, etc.)
install python keyboard library https://pypi.org/project/keyboard/
run below in python console
>>> import keyboard
>>> keyboard.press_and_release('Q')
>>> Q
In the above, Q was rebinded to A in the hotkey application but the code still outputted Q.
Is it possible to send actual key press events as though the physical buttons are pressed (so that the hotkey is triggered) and Q will type A instead? And to capture the key press (A) and store it in a string so that I can use this to assert whether the hotkey is activated. The code should work regardless of what hotkey program I use to rebind a key.
So I'm trying to get a Python script to detect if I've pressed insert and if I have it should print the clipboard to a file.
I've got most of it working but it seems the game eats up my keyboard detection.
I currently have:
keyboard.add_hotkey('k',writeToFile,args=[])
using the keyboard module, but it does nothing when in game. Outside the game it works fine.
As the README says:
Other applications, such as some games, may register hooks that swallow all key events. In this case keyboard will be unable to report events.
If keyboard can't report the events, it can't use them to trigger a hotkey.
It may be possible to get around it by just waiting until the game has started, and only then starting your script. If there's no way to tab out of the game without quitting, of course, this is obviously a non-starter—but even if you can do that, it still may not work.
And if not, you just can't hook keyboard events with a global event hook. Which means there's nothing keyboard can do for you.
But, since you're dealing with an 18-year-old game here, there might be a workaround: You may be able to run the game inside an emulator or virtual machine, smoothly enough to be perfectly playable. If so, the game should only be able to globally hook events inside the emulator/VM, not on the real system, so your hook may be able to catch events first. (You may still need to run your script last, or you may need to configure your Wine/VMware/whatever settings, but it may just work out of the box.)
TL;DR, I'm trying to make a python-only single-module platform-nonspecific non-blocking keypress detector that works in IDLE.
Good luck.
So say I'm running a loop, in IDLE, for an arbitrary amount of time. I want a way to get user input without blocking the loop, on any system I want.
Example:
import keyboard #(is one module in same directory)(Mac, Windows, Linux, who knows?)
teststring = ''
while True:
if keyboard.isDown('a'):
teststring += 'a'
if keyboard.isDown('enter'):
if 'a' in testring:
break
... (neural network or whatever continues)
To put it a different way, I'm trying to make a universal event listener that works in IDLE. If one already exists, do let me know, but I have done some research and none work in one easy-to-use module.
Note:
You don't have to write this, I just want to know what pre-installed Python modules you would use to do this and a general idea of how to go about it.
My question is, is there any way to reliably hold a key down on a mac with applescript/python?
I have search almost everywhere for topics like this, however, none of them cut it. I am attempting to use Python to read serial information from an Arduino, then relay those signals as keypresses. I have seen how to use applescript to send a "key down" to the system event, as shown in the following code:
(Python code)
def SendDown(key):
string = str(key)
cmd = """osascript -e 'tell application "System Events" to key down (key code """ + string + ")'"
os.system(cmd)
This code works generally, however, I want to control the Google flight simulator. When I attempt to do this, the key presses seem to be way to quick and the flight simulator or the basic google earth map moves fractions of what I would expect.
They way I am using this code is essentially as follows (suedocode)
if (ArduinoMessage == "left"):
SendDown(leftKey) #leftKey has been set to 123 -- the code for the left arrow key
etc...
From my point of view, the key down event I'm sending is essentially a quick keystroke and the key is not being held down. I tried programming the key event directly in applescript and had a little bit of success. My code looked something like this:
tell application "System Events"
repeat 50 times
key down (key code 123)
end repeat
key up (key code 123)
end tell
That code moved the google earth map more than I have been getting, but it took a long time to get it to move a small amount (far less than the normal arrow keys). Then I tried to write that applescript into Python and lost all improvement.
So I restate the question -- is there any way to reliably hold a key down on a mac with applescript/python?
I was able to get this to work on Windows fairly easily, however, I was able to use a Windows only library called SendKeys that is designed for applications such as this.
Any help would be appreciated.
Thanks,
Jake
Use PyUserInput.
Try following code.
import time
import pykeyboard
# TODO: Replace following two lines with the code that activate the application.
print('Activate the application 3 seconds.')
time.sleep(3)
k = pykeyboard.PyKeyboard()
k.press_key(k.left_key)
time.sleep(1) # Hold down left key for 1 second.
k.release_key(k.left_key)
Unfortunately I don't have Mac. I tested in Linux, Windows.
I wanna setup a global hotkey in python 2.6 that listens to the keyboard shortcut ctrl + D or ctrl+ alt+ D on windows, please help me
Tim Golden's python/win32 site is a useful resource for win32 related programming in python. In particular, this example should help:
Catch system-wide hotkeys
I suggest pyhk. It allows for global wide hotkey registration in python and comes with examples and documentation. Pyhk builds on pyhook.
Hotkey registration is as simple as:
pyhk.addHotkey(SomeHotkey,SomeFunction)
The RegisterHotKey method of the wx.Window class is what you're looking for -- as the docs say,
Registers a system wide hotkey. Every
time the user presses the hotkey
registered here, this window will
receive a hotkey event. It will
receive the event even if the
application is in the background and
does not have the input focus because
the user is working with some other
application. To bind an event handler
function to this hotkey use EVT_HOTKEY
with an id equal to hotkeyId. Returns
True if the hotkey was registered
successfully.
So, make an instance of `wx.Window, register the hotkey you want with this method, and possibly do a PushEventHandler if ypu'd rather handle the event(s) in a separate event handler rather than in the window itself (the latter being the default).
Is there anything else in this procedure that is not entirely clear to you...? If so, please edit your question to add whatever further problems you may have!
If you want hotkeys in your wxPython program (which I assume you do because of the wxPython tag), then you should use a wx.AcceleratorTable.