Python catch keyboard input in the background and process it - python

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 ?

Related

Python keyboard input while blocking keyboard for the rest of the system

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)

is there a way to map all keys from the keyboard to (some char) with ctypes in Python?

from ctypes import *
ok = windll.user32.BlockInput(True)
So I learned that I can block input in windows with that code
Is it disabled when the script ends or when the pc is rebooted?
But instead of blocking all input id like to redirect all inputs to a char as "E" or "5" does anyone know how to do it?
According to [MS.Docs]: BlockInput function:
The system will unblock input in the following cases:
The thread that blocked input unexpectedly exits without calling BlockInput with fBlock set to FALSE. In this case, the system cleans up properly and re-enables input.
The user presses CTRL+ALT+DEL or the system invokes the Hard System Error modal message box (for example, when a program faults or a device fails).
So, unblocking occurs when the script (Python process (and its main thread) that executes the script) ends.
For generating system events you should check SendInput documentation. For more info, also check [MS.Docs]: About Keyboard Input.
Regarding key "redirection", I don't know a(n easy) way, but it looks like an XY Problem. There should be another (simpler) way of achieving your end goal.

Python keyboard events - Hide user input and send keys to buffer

Hi,
I am working on a project and I am monitoring the keyboard with the keyboard module.
My application works in real-time (on-the-fly) and read strings entered from user (with the keyboard module as mentioned)
What I want to do is hide user input when some specific conditions are True.
I have searched all the web and didn't manage to find something that does what I want.
To give it a more detailed explanation lets say that the user enters some text and this text string-by-string is being checked for some condition from my program.
If everything is OK, then nothing happens but if not, then I want the next user input not to be shown in the position he is writing.
I found solutions that do exactly this in the terminal like the msvcrt module (How to temporarily disable keyboard input using Python ) or do the above functionality with the input() function.
Is there something that prevent the text ,entered from the keyboard, from showing to the screen, but send it to a buffer for editing first.
Thanks in advance.
! I am on windows

How to send raw input as if from keyboard or mouse?

I am trying to write a macro using python.
I want it to do a repeated task within another program.
I believe the program that I am attempting write this macro only
accepts raw input from keyboard and mouse input stream.
Using python libraries such as pynput.mouse, mouse, pyautogui, so on
seem to be sending a different type of keyboard/mouse input that the program
will not recognize. (Those libraries do work to control and move the mouse
around my computer screen but do not work in the program)
It was suggested that the program accepts only input directly from the keyboard
and mouse (ports?).
How can I write code that sends data as "raw" to the program, or reroute it through the correct (port?) input stream so that the program reads it as "raw"
I hope I was able to explain this clearly.
Please see the following post: ctypes mouse_events
It explains how to do this with raw win32 messages which will do the trick.

How can we generate Keyboard interrupts using python

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)

Categories

Resources