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.
Related
I use python and pynput to automate my mouse.
But obviously, it is impossible to use the computer at the same time for other things. So I'm looking for a solution to either automate a "second" virtual mouse or to just send mouse clicks to a specific window (active or inactive) on Windows 10 without actually using the real mouse.
You can use pyautogui to automate keyboard and mouse actions; but if you are using the keyboard and mouse it will interfere with these commands. the same could be said about adding another mouse; it doesn't add a second OS pointer; it only creates another (at times conflicting) control over that pointer.
I have written a program which opens up adobe acrobat and rapidly clicks a bunch of buttons in the user-interface. The program also types some stuff. The problem is that if a user accidentally presses a keyboard key, or twitches the mouse while the program is running, then it can mess-up the results.
The desired behavior is to to suppress/ignore input from all keyboards and mice but one pair. The escape key will still be allowed on all system keyboards. The non-disabled keyboard and mouse are virtual, not physical. This non-disabled mouse and keyboard will do all of the procedural-generated clicking and typing. If any keyboard (including disabled ones) has an escape key press event, that input will not be suppressed. I will bind some sort of "pause program" feature to it.
You may assume that the os is Windows. How do we write code in python for this?
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
I'm developing an application in python which sends keyboard events to another external application. I use the pywin32 package to set the external application and send the desired key:
import win32com.client as w32
shell = w32.Dispatch("WScript.Shell")
shell.AppActivate(desired_application)
shell.SendKeys("{ENTER}")
The external application I'm using has a virtual keyboard and a text area which receives the events of the keyboard. I want to send the key event (in this case, an 'ENTER') to the keyboard area (because the keyboard is making a scan through the letters and will select the desired letter with an Enter). However, my application is sending the key events to the text area instead of the keyboard.
I tried to get the handle of the window I want with FindWindow and EnumChildWindow from win32gui... So, is there a way to send the keys to the specific child window of the external application?
I manage to choose the specific handle with EnumChildWindow (to enumerate all the handles of the application) and send the message with PostMessage.
import win32api
win32api.PostMessage(handler, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
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.