Grab keyboard output - python

I want to find a way in Python for detecting "Keyboard event" like PyUserInput. I dont want the keyboard to type any character after pressing a button, and ONLY call me event occured.
For example, after pressing the "s" key run the event handler but not typing "S"! this action should be applied in all over the windows not frames such as "Tkinter"
(I want to grab keyboard output signal by python)

What you're trying to do isn't feasible with python.
What you want to do is essentially make a new driver for your mouse/keyboard.
This is not system independent and there's no good/nice way of doing this with python.
PyUserInput attempts to abstract out the system dependency - but this is not complete, and it cannot override the keyboard/mouse completely. What it can do is it can mimic actions on your mouse or keyboard only if the application allows emulated actions. A lot of programs like games and a bunch of other software disable emulated actions.
The most stable and significant tool which is not a driver level tool are autoit and autohotkey - you could try making an autoit/autohotkey script which is executed as a python subprocess and use pipes to send and get info from the script.

Related

Send keystrokes to non-active GUI application without occupying the keyboard

As the title explain, i'm trying to use the terminal to send commands as keystrokes to a GUI application that's minimized.
There is a lot of similar questions here on Stack with some great answers, but i'm having, mainly, three problems with the solutions i saw: Most of the solutions need the automated application to be the active one. Or, i can't normally use my keyboard while the script/process is running. Or worse, the solution works only on Windows OS.
I need what this person asked 2 months ago: Send keystrokes to a specific window (in background), but do something else in the meantime
But i want it on Linux.
I'm using Kubuntu 18.10, if that helps.
xdotool was close, but i couldn't quite get it to send the commands to a specific window or PID. It also uses "my keyboard", so i can't, for example, write an essay/code/browse online while xdotool is running. Pexpect also have this last problem.
AutoHotKey looks like it would work, but it's only for Windows and i'm trying to not use Wine. Same with pywin32.
keyboard (https://github.com/boppreh/keyboard) seems nice, but it can't send a command to a specific application. Same with PyAutoGUI.
I selected the Python tag because most of the solutions i saw use Python, but i'm open to any language.
Use a nested X server to input keystrokes without changing focus or keyboard grab.
Proof of concept:
Xephyr -resizeable :13
export DISPLAY=:13
xterm
xdotool type rhabarber
The Xephyr nested X server is started and will listen on local X socket 13 (whereas :0 typically identifies the currently running X server, but when multiple sessions are ran concurrently, it could be higher).
Then we set DISPLAY environment variable to :13, so any X application we start will connect to Xephyr; xterm is our target application here. Using xdotool or any other tool we can send keystrokes.
As the target X server is identified through $DISPLAY, applications can be started or input events triggered from elsewhere as well. If needed, you might also run a lightweight window manager within Xephyr, e.g. to 'maximize' the application so that it fills the whole Xephyr window.

Win32API Mouse vs Real Mouse Click

I have recently started using win32api to simulate mouse events and was wondering if it was at all detectable?
For example, does the api follow the exact same process/run the exact same commands as if done when using a real mouse - or are there some slight differences which can be detected? Furthermore, is this the same case with win32com SendKeys (via Shell Script/Python)?
I ask, because in the past I have had a few applications detect the Java robot library - but they all seem to work fine when using the Python win32api. Thanks.
The SendInput function will insert input events into the same queue as a hardware device but the events are marked with a LLMHF_INJECTED flag that can be detected by hooks. To avoid this flag you probably have to write a custom driver.

Capture and consume input events from background python process without notifying focused window

Basically I would like to write small script that would allow me to have some sort of programmable keyboard emulation. Something similar to how autohotkey on Windows is able to work.
Lets say I would like to rebind arrow keys to 'wsad' or 'hjkl' but only when CapsLook is active. I was able to detect keyboard key press with pyinput(https://pypi.python.org/pypi/pynput ) I also can send easily various keyboard events to focused window with pyautogui (https://pyautogui.readthedocs.io) But I can't figure out a way to consume events before they are received by currently focused window.
Any hints?
THIS module is one of the available tools for capturing keyboard events:
https://pypi.python.org/pypi/keyboard/
but it is still in the development and doesn't (yet) provide a global hook capable of capturing keyboard events at their very origin and forwarding them (or not) to the target application.
Another tool worth to look into is:
myboard.py at code.google.com downloads
The above script is using Python ctypes and Xlib modules which makes it possible to work directly with the system libraries written in C. It catches the keyboard events quite deep and system wide to a degree that it had crashed my OS while testing it a bit, so be warned ...
Consider also using XGrabKey and XGrabKeyboard from the X11 libX11.so system library:
import ctypes
libX11 = ctypes.CDLL('libX11.so')
XGrabKey = libX11.XGrabKey
XGrabKeyboard = libX11.XGrabKeyboard
print("XGrabKey: " , dir(XGrabKey))
print("XGrabKeyboard: ", dir(XGrabKeyboard))

How to get python to constantly check for key combinations even if it is in the background?

I was thinking of coding a script that will run in the background of my computer that will detect key combinations to start specific tasks.
For example, the user presses Ctrl+Alt+F1 then the browser starts. However, I cannot seem to find a way to detect these combinations while the script is running in the background.
I thought of using a .pyw extension for my script, but that makes the script not interact-able.
If anyone needs to know, my OS is Windows 10.
I think the pyHook library is what you're looking for:
The pyHook package provides callbacks for global mouse and keyboard events in Windows. Python applications register event handlers for user input events such as left mouse down, left mouse up, key down, etc. and set the keyboard and/or mouse hook.

How to send a keyboard event to a different Window in Pywin32?

I have to write a script to emulate some keyboard event in a different program in background.This is my codeļ¼š
pwin = win32ui.FindWindow(None,r'someprograme')
pwin.SendMessage(win32con.WM_KEYDOWN,18)
pwin.SendMessage(win32con.WM_KEYDOWN,68)
pwin.SendMessage(win32con.WM_KEYUP,18)
pwin.SendMessage(win32con.WM_KEYUP,68)
pwin.SendMessage(win32con.WM_KEYDOWN,13)
pwin.SendMessage(win32con.WM_KEYUP,13)
But it seems nothing happened.So what should i do?I've tried PostMessage func it seems it still can not do it.
After a quick look at the WM_KEYDOWN docs:
Posted to the window with the keyboard focus when a nonsystem key is pressed. A nonsystem key is a key that is pressed when the ALT key is not pressed.
But looking up your keycodes, you're trying to send ALT-D (followed by ENTER, which is fine). It sounds like you're trying to drive the menus; if that's what you want to do, WM_KEYDOWN is not the way to do it.
The problem is that keyboard menu navigation is driven by Windows, not by the app (except for a handful of apps that override normal menu processing, like some versions of Visual Studio). When you're looking at Notepad, and you hit ALT-F to open the File menu, the Notepad code gets a bunch of menu-related messages (WM_INITMENU, etc.), not the keystrokes.
If you use a WM spy program (I think the free Visual Studio Express still comes with Spy++ and ManagedSpy, but if not, search for an equivalent), you can see what the application is actually seeing when you drive it with the keyboard, and then you can figure out how to emulate that from your Python script.
On top of everything else, depending on how the program is written, it may not accept keystrokes when it doesn't have focus.
By the way, if you're just getting started with Windows GUI automation, you might want to look at something higher level, like pywinauto. That way, you don't have to work out what menu-related messages to send to open the Data menu; you just do something like app.Foo.MenuSelect('Data').

Categories

Resources