Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What key pressed?
#1
I test the code below but not sure how to catch non-ASCII key, like Enter, Esc and so on. Thanks

import msvcrt
while True:
    if msvcrt.kbhit():
        print(msvcrt.getch())
Reply
#2
Try playing with this:

import msvcrt
while True:
    if msvcrt.kbhit():
        print(repr(msvcrt.getch()))
Some trial and error should get you the info you need.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
>>> import msvcrt
>>> help(msvcrt.getch)
Help on built-in function getch in module msvcrt:
 
getch()
    Read a keypress and return the resulting character as a byte string.
     
    Nothing is echoed to the console. This call will block if a keypress is
    not already available, but will not wait for Enter to be pressed. If the
    pressed key was a special function key, this will return '\000' or
    '\xe0'; the next call will return the keycode. The Control-C keypress
    cannot be read with this function.
Can do a test,use repr() to see what really going on.
import msvcrt
 
while True:
    character = msvcrt.getch()
    print(repr(character))
    if character == b'q':
        break
So i get b'\x00' normal key and b'\xe0' for function keys.
Then can write it like this.
import msvcrt
import sys

while True:
    ch = msvcrt.getch()
    if ch in b'\x00':
        ch = msvcrt.getch() # Second call returns the scan code
    if ch in b'\xe0':
        ch = msvcrt.getch() # Second call Function keys
    if ch == b'q':
       sys.exit()
    else:
       print(f'Key Pressed: {ch}')
Output:
E:\div_code λ python scan.py Key Pressed: b'h' Key Pressed: b'e' Key Pressed: b'l' Key Pressed: b'l' Key Pressed: b'o' Key Pressed: b'\x1b Key Pressed: b'\x1b Key Pressed: b'\x1b Key Pressed: b'\r' Key Pressed: b'\r' Key Pressed: b'\r'
3 times Esc and 3 times Enter after hello.
Key Scan Codes
So Esc(27) and Enter(13).
>>> k = b'\x1b'
>>> ord(k)
27
>>> k = b'\r'
>>> ord(k)
13
>>> if ord(b'\r') == 13:
...     print('Enter key was pressed')
...     
Enter key was pressed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting button pressed number Moris526 4 2,494 Dec-14-2020, 01:41 AM
Last Post: Moris526
  How many times was the button pressed in pyglet rama27 0 1,948 Oct-10-2020, 10:26 AM
Last Post: rama27
  run different functions each time the same button is pressed? User3000 6 3,396 Jul-31-2020, 11:11 PM
Last Post: User3000
  Terminate a process when hotkey is pressed 66Gramms 0 2,279 Dec-24-2019, 06:41 PM
Last Post: 66Gramms
  Advance program when certain keys are pressed? Chrislw324 2 2,377 May-19-2019, 07:13 PM
Last Post: woooee
  Count to movement according to the time pressed button noartist 1 2,557 Feb-27-2019, 01:33 PM
Last Post: noartist
  Can't edit code after I've pressed enter. xBlackHeartx 2 12,170 Sep-02-2017, 10:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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