3

I am currently writing a program for a fake code/hacker typer which when in python and keys are pressed it prints out source code which makes the user looks like they are typing.

I know using the keyboard module you can use

import keyboard

keyboard.if_pressed(hotkey)

but how would one use this without specifying any key just if any key is pressed on the keyboard?

Bhavya Parikh
  • 3,304
  • 2
  • 9
  • 19
Amin Sahid
  • 31
  • 1
  • You could be talking about two different things. If you're talking about grabbing keystrokes from the current terminal session, then the `msvcrt` answer will work on Windows, and there are ways to do it on Linux using "raw mode" input. If you're talking about grabbing keystrokes systemwide, even when your app does not have focus, that a harder task that requires system hooks. – Tim Roberts May 04 '21 at 03:18
  • @TimRoberts I need something that will interact with the terminal regardless of the operating system used, but to clarify not something that would grab keystrokes systemwide. – Amin Sahid May 04 '21 at 03:21
  • 1
    Does this answer your question? [How to read a single character from the user?](https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user) – Anon Coward May 04 '21 at 03:24
  • Does this answer your question? https://stackoverflow.com/a/53210441/11915854 – Sid May 04 '21 at 03:44

1 Answers1

0
import msvcrt
while True:
    if msvcrt.kbhit():
        key_stroke = msvcrt.getch()
        if key_stroke:
            print("Clicked")
Sid
  • 2,174
  • 1
  • 13
  • 29