Attempting to read keyboard outside of console - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Attempting to read keyboard outside of console (/thread-38735.html) |
Attempting to read keyboard outside of console - Jimmy998 - Nov-18-2022 You know the input function? where you store a string into a variable? Yeah, I don't need that, for at least that does not benefit me. My attempt is to read keyboard strokes without having the need of being in the console/terminal. I want to be able to change my output when I type my keyboard, almost creating a new keyboard layout using python. I need help on gathering keyboard input, and if possible, please also include how to print text onto where my cursor is at. I hope to use default python functions to achieve this, if impossible, see if possible to use python default modules to achieve this. Thanks in advance! RE: Attempting to read keyboard outside of console - Larz60+ - Nov-18-2022 for starters, look here: https://pypi.org/project/keyboard/ There are othre packages available, see: https://pypi.org/search/?q=keyboard RE: Attempting to read keyboard outside of console - Jimmy998 - Nov-19-2022 Please, is there something of a default module? RE: Attempting to read keyboard outside of console - Larz60+ - Nov-19-2022 the link I show above is a default package, which can be used in your code using import. If you don't like the 'keyboard' package, you can choose another. You then use tha package directly in your code, no need to go to terminal (after install) If you were to choose the 'keyboard' module, you would:
RE: Attempting to read keyboard outside of console - Jimmy998 - Nov-25-2022 Default as in I don't need to install using pip. like OS is a built in module RE: Attempting to read keyboard outside of console - Larz60+ - Nov-25-2022 OK, I get it. Here's code that will capture Ctrl-c allowing you to clean up anything needed after Ctrl-c is pressed, then exit the program gracefully import time import sys def catch_ctrlc(): try: while True: # program code goes here print("Running program commands") time.sleep(2) except KeyboardInterrupt: print("Captured keyboard Ctrl-c") print("I will exit the program now") sys.exit(0) finally: print("Do any cleanup here") if __name__ == '__main__': catch_ctrlc()running: If you want to catch other characters, you can do it with builtin curses,see: https://docs.python.org/3/library/curses.html |