Python Forum
KEY input.... - 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: KEY input.... (/thread-39444.html)



KEY input.... - ATARI_LIVE - Feb-19-2023

I am doing code to read a key is pressed.

The KEYBOARD module is works but it will read keys even I use another window-box which I do not want.

I looked up online and seem off my points, they talk about wait for ENTER key and they keep talk about KEYBOARD.

I am looking for to read a key when key is pressed and get the value with in same the window-box.

Any idea?


RE: KEY input.... - Larz60+ - Feb-19-2023

Please show your code so far (please use bbcode tags)
Difficult to advise otherwise.


RE: KEY input.... - ATARI_LIVE - Feb-19-2023

deleted


RE: KEY input.... - ATARI_LIVE - Feb-20-2023

Delete


RE: KEY input.... - ATARI_LIVE - Feb-20-2023

(Feb-19-2023, 02:04 PM)Larz60+ Wrote: Please show your code so far (please use bbcode tags)
Difficult to advise otherwise.

import keyboard
loopstop=1
 
while loopstop:
    keypress = keyboard.read_key()
    if keypress != 0:
        print(keypress)
    if keypress == "esc":
        loopstop=0
This code what i have problem is it will read the key anywhere in OS like I use CMD and it keep reading like key logger which I do not want.
Also another problem is when I pressed, it read and printed then I release key then it ready again. like double printed.


RE: KEY input.... - ATARI_LIVE - Feb-20-2023

(Feb-20-2023, 12:37 AM)ATARI_LIVE Wrote:
(Feb-19-2023, 02:04 PM)Larz60+ Wrote: Please show your code so far (please use bbcode tags)
Difficult to advise otherwise.

import keyboard
loopstop=1
 
while loopstop:
    keypress = keyboard.read_key()
    if keypress != 0:
        print(keypress)
    if keypress == "esc":
        loopstop=0
This code what i have problem is it will read the key anywhere in OS like I use CMD and it keep reading like key logger which I do not want.
Also another problem is when I pressed, it read and printed then I release key then it ready again. like double printed.

I got it resloved!
import msvcrt
while True:
   if msvcrt.kbhit():
      key = msvcrt.getch()
      print(key.decode(), end='', flush=True)