Python: Reading input when finished typing - python

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.

Related

How to save tkinter dialog data?

I want the user to enter something in the TextBox, and if he exits the program and returns to open the program again at any time, he finds that the words he entered are still there.
(Python programmers - Save Dialog- tkinter )
If you want to save the state of the program, you could save it in file, in a database, or put your data in one place and use pickle. And at least reload the state on program start.

Can I put input in Mindstorms 51515 while programming in python?

When I was programming in Mindstorms Robot Inventor, and decided to put an input in my code, I bumped into this NameErrorThis is the Error
Is it Even possible to put an input in Mindstorms??
The Python available isn’t the complete Python standard library. The terminal view in the code editor doesn’t provide for a way to put in input either, so unfortunately no.
What you can do instead is use the buttons on the hub as a type of input. This wouldn’t be free-form text, but if you’re using input() as a user-controlled wait before progressing, you can do that.
_ = hub.right_button.was_pressed() # clear out any button press
while not hub.right_button.was_pressed():
# do something
# do something after the button was pressed
You can string multiple of the above in a row if you want to do multiple actions separated by waiting for you to let it continue, or even take different actions based on which button was pressed.
Finally, you could do a very slow text input using the buttons and the display screen to scroll through the alphabet, confirm the letter, and finish the input.

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.

Python redirect stdin

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.

Categories

Resources