Python redirect stdin - python

I'm trying to set up a barcode scanner object that will capture anything input from the scanner itself. The barcode scanner is recognized as a standard input (stdin) and therefore whenever I scan a barcode, I get standard input text. There will also be a keyboard attached to the system, which is another standard input. In order to differentiate between a barcode scan input and keyboard input, I will be using a prefix for any barcode information. In other words, if my barcodes will be 16 characters in length total, the first 4 would a predetermined character string/key to indicate that the following 12 characters are barcode inputs. This is pretty standard from what I've read.
Now most examples I've seen will recognize the barcode input by capturing the character input event in a GUI application. This event callback method then builds a buffer to check for the 4 character prefix and redirects barcode input as necessary. The event callback method also will skip any character input events that are not barcode related and allow them to interact with the GUI as a standard input normally would (type into a text box or what have you).
I want to do this same thing except without using a GUI application. I want my barcode scanner object to be independent of the GUI application. Ideally I would have a callback method, within the barcode scanner object, that stdin would call every time a character is input. From there I would grab any barcode input by checking for the 4 character prefix and would pass along any characters not apart of the barcode input. So in other words, I'd like stdin to pipe through my barcode scanner callback method, and then have my barcode scanner call back method be able to pipe non barcode characters back out as a standard input as though nothing had happened (still standard input that would go to a text box or something).
Is this possible without a while loop constantly monitoring stdin? Even if I had a while loop monitoring stdin, how would I pump characters back out as stdin if they weren't barcode input? I looked into using pyusb to take over the barcode scanner's USB interface, but this requires root privileges to interact with the hardware (not an option for my project). Any help would be greatly appreciated. I have not been able to find an example of this yet.
Edit: This project will be run in CentOS or some flavor of Linux.

The normal way of intercepting standard input in Unix is pipes and multiple processes. If you have a multi-process application, then one process can receive the "raw" standard input, capture barcode input, and pass on the rest to its standard output. That output would then be the standard input of your UI process which would only receive non-barcode data. To set this up initially, have a single launch process that sets up the pipes, starts the other two processes, and exits.
If you're new to these concepts, you have a long and interesting learning process ahead of you :-)
All this assumes that you really are receiving "keyboard" data through standard input, and not through X11 events as you seem to imply. If you are developing within X11 (or GTK, etc.) then what I have described will almost certainly not work.

Related

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.

Keyboard output to multiple programs simultaneously?

My issue currently is that of emulating keystroke input to an arbitrary program (such as a running game).
Currently I am using win32 libraries on Windows to find windows (win32gui.FindWindow) and grab focus (via win32gui.SetForegroundWindow), then send keyboard input (win32api.keybd_event).
This works if I am only sending input to a single program, but I wish to parallelize, playing multiple games simultaneously. This does not work with my current method as both applications demand "focus" for the keys to go to the right application, thus interfering with each other.
Ideally I would like something that sends input to a given window, that does not require focus , and is independent of input given to other windows or the currently focused window.
My understanding is, only the foreground window get the focus and can handle keyboard input to play. Not sure in make sense to send input to background window or not....

Python: Reading input when finished typing

I am writing a python script which is going to take use of input from a barcode scanner. As it stands, the barcode scanner acts as a keyboard, writing the scanned code into the console (such a code may for ex. be: 123456789). Is there a way to automatically read the inputted code when the scanner is finished writing? Right now the user has to press enter any time a code is scanned. Are there any existing libraries for barcode scanners that i have yet to come accross?
Reading the input with raw_input will not work because it waits for the user hitting the return button. However, sys.stdin.read() reads single characters from the standard input. Use this function and check if the characters that were already entered are as many as you expect.
Information about read(): https://docs.python.org/2/library/stdtypes.html?highlight=read#file.read. sys.stdin works like a file.

Categories

Resources