Is there a way to generate Keyboard interrupt without actually pressing a key on keyboard using a python program in linux. The interrupt should look like if someone has pressed a key on the keyboard and the complete system should get that interrupt irrespective of the currently focused window. For example if I have opened an editor and my python program generates an interrupt for key "A", then "A" should get printed in the editor.
python-uinput
Here is the example:
import uinput
device = uinput.Device([
uinput.KEY_E,
uinput.KEY_H,
uinput.KEY_L,
uinput.KEY_O,
])
device.emit_click(uinput.KEY_H)
device.emit_click(uinput.KEY_E)
device.emit_click(uinput.KEY_L)
device.emit_click(uinput.KEY_L)
device.emit_click(uinput.KEY_O)
Related
I want my configuration menu to open at the start of a script if the Ctrl key is being pressed. That is, the user would start pressing the key before the script starts running.
To intercept that I have tried keyboard.is_pressed('ctrl'), but it always returns false.
I have also tried keyboard.read_key but this method gives me two problems:
If it is not pressed, the program does not continue running.
If it is pressed, it just continues running only when I release the control key.
Maybe I just need to think of another way to open this menu.
I'm assuming you're using the keyboard module for this. Looking through the code, it seems like that library is event based, so if the CTRL key is pressed before your program starts, then it isn't recognized by the program.
My recommendation would be to read directly from stdin using the sys library. However, looking at How to get Ctrl, Shift or Alt with getch() ncurses?, it seems like CTRL, ALT, and SHIFT don't generate their own input to stdin but rather modify existing input, so you should probably look at using another key as your modifier. You also need a timeout, which Read file with timeout in Python suggests the select module provides. In case you don't know already, sys.stdin is your standard input, and you can treat it like any other file.
Assuming my code is going to run on Windows and thanks to my experience in C++ I came up with the win32api module.
# Windows module to controll the keyboard
import win32api
# Windows module with every key code
import win32con
# Check whether the control key is pressed
if win32api.GetAsyncKeyState(win32con.VK_CONTROL) & 0x8000 > 0:
# Do stuff
I'm currently working on a python program that is supposed to catch all keyboard input and then depending on the input given emulate a keyboard input
For example, I type the letter a into for example windows notepad
my python program catches this returns an emulated keyboard input for example b
what would be the best way to catch the keyboard input?
I looked into the possibility of using a modified keylogger however I ran into an issue how do I stop for example character a from being typed ?
Premise:
I am trying to have python accept keyboard input and have it lock the keyboard to only python input such as that I can hit a key and "transfer" the keyboard from the entire system to only my python script and have it listen to any keys hit so there's no input terminal or any sort of GUI for input
Question:
Is there any way of locking keyboard input to only python for a short time? Is there a better way of doing this?
More Informations:
I want to have the script "draw", on the screen, when a combination of keys is pressed, for example having ctrl be a square, I can use keyboard.wait() to register the hotkey or a combination of them and then just drawSquare() using pyautogui or pywin32 or any other means but this would mean that the size of the square, the movement of the mouse would be hardcoded
This I am trying to avoid, I had in mind waiting for a hotkey with keyboard.wait() then just listening for, say, size of square, or coordinates, whatever, and this does work but the problem is keyboard also "types" in whatever program I am as well as python (using keyboard.record()) so say I wanted ctrl to begin listening for size and then enter 123 123 for width and height 123 123 would also be entered into whatever program I am in (as it would with natural typing since keyboards are meant to do that)
I am iterating through files in a folder, and for each file I want to be able to press either "y" or "n" on the keyboard (not in the terminal, just listen to the keystroke event) regardless of the focus window and do something with that information. I.e. for each iteration, I don't want to have to move my mouse to the terminal, click it, press "y" and press enter. Just n or y instantly and move to the next iteration. Is this possible and if so how do I do it? It is on linux if it matters.
you need some kind of keyboard event listener
i think Tkinter has it
otherwise use some other python library
1 example: Detecting a keypress in python while in the background
For a personal project, I want to generate keyboard presses for a N64 emulator. It seems like it recognizes only one keyboard event when I select the Python IDE then switch to the emulator. I want the emulator to constantly recognize the button presses the python script generates. The code is really simple, it constantly replicates the x button being pressed.
import serial,win32api, win32con,time
while 1:
win32api.keybd_event(0x58,0x2D,0,0)
time.sleep(1)