Python Forum
Attempting to read keyboard outside of console
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attempting to read keyboard outside of console
#1
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!
Reply
#2
for starters, look here: https://pypi.org/project/keyboard/
There are othre packages available, see: https://pypi.org/search/?q=keyboard
Reply
#3
Please, is there something of a default module?
Reply
#4
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:
  1. Install keyboard package (from command line) with pip install keyboard This is a one time operation.
  2. import into your code, using import keyboard whenever code requires it (once at start of code)
  3. Use provided methods in your own code, see examples here: https://github.com/boppreh/keyboard
Reply
#5
Default as in I don't need to install using pip. like OS is a built in module
Reply
#6
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:
Output:
Running program commands Running program commands Running program commands Running program commands Running program commands ^CCaptured keyboard Ctrl-c I will exit the program now Do any cleanup here
If you want to catch other characters, you can do it with builtin curses,
see: https://docs.python.org/3/library/curses.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug beginner attempting to make chatbot MonikaDreemur 1 650 Feb-02-2022, 10:24 PM
Last Post: BashBedlam
  Newbie to Python (Attempting to install DJango on a VPS) jarjar95 0 1,104 May-04-2021, 03:51 PM
Last Post: jarjar95
  List index out of range error when attempting to make a basic shift code djwilson0495 4 2,137 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Invalid archive error when attempting to install dash bootstrap components meaydemi 0 4,236 Jul-11-2019, 05:49 PM
Last Post: meaydemi
  How do I read the keyboard arrow keys? r2d2 0 4,824 Feb-16-2019, 07:01 PM
Last Post: r2d2
  attempting to run gimp-console from python in windows 10 matteusbeus 3 2,247 Oct-09-2018, 07:05 AM
Last Post: matteusbeus
  Attempting to run py2 and py3 on a windows box Vysero 5 2,941 Jun-26-2018, 06:23 PM
Last Post: snippsat
  Attempting to port XTea Encryption from C to Python sonic1015 1 2,794 Jun-06-2017, 07:12 PM
Last Post: sonic1015
  Attempting to get pkg-config working Able98 0 2,565 Apr-02-2017, 03:51 PM
Last Post: Able98
  Automatically read copied text from keyboard shortcuts Ambidexter2017 10 9,190 Mar-10-2017, 01:46 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020