Get keyboard events with python

I want to get a keyboard event

I want to output something when the user enters a specific key

code

from msvcrt import getch

def select():
    #do something

def moveDown():
    #do something

def moveUp():
    #do something

def main3():
    while True:
        key = ord(getch())
        if key == 27: #escape
            break
        elif key == 13: #enter
            select()

        elif key == 224: #Special keys (arrows, F keys, ins, del, etc.)
            key = ord(getch())
            if key == 80: #Up arrow
                moveDown()
            elif key == 72: #Down arrow
                moveUp()

ascii code correspondence table

http://www.theasciicode.com.ar/ascii-control-characters/escape-ascii-code-27.html

What I noticed

--Responds only on the command prompt (I wanted the input on any window to respond) --Window OS only

Reference material

http://stackoverflow.com/questions/12175964/python-method-for-reading-keypress

Recommended Posts

Get keyboard events with python
Get date with python
Get country code with python
Get Twitter timeline with python
Get Youtube data with python
Get thread ID with python
Get started with Python! ~ ② Grammar ~
Get stock price with Python
Get Alembic information with Python
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get reviews with python googlemap api
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
[Python] Get economic data with DataReader
How to get started with Python
[Small story] Get timestamp with Python
Get Qiita trends with Python scraping
Get started with Python in Blender
Get weather information with Python & scraping
Get additional data in LDAP with python
Get property information by scraping with python
FizzBuzz with Python3
Get html from element with Python selenium
Scraping with Python
[Note] Get data from PostgreSQL with Python
Statistics with python
[Automation] Manipulate mouse and keyboard with Python
Scraping with Python
[Python] Get the variable name with str
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
Get Started with TopCoder in Python (2020 Edition)
Bingo with python
Zundokokiyoshi with python
[Python] Get Python package information with PyPI API
How Python beginners get started with Python with Progete
[ev3dev × Python] SSH Control (remote control with keyboard)
Let's get along with Python # 0 (Environment construction)
Excel with Python
Microcomputer with Python
[Blender x Python] Let's get started with Blender Python !!
Cast with python
Python hand play (let's get started with AtCoder?)
[Python] Get the files in a folder with Python
Get a ticket for a theme park with python
Get stock price data with Quandl API [Python]
I tried to get CloudWatch data with Python
Get git branch name and tag name with python
[Python] Get rid of dating with regular expressions
[python] [vscode] When you get angry with space-tab-mixed
Get CPU information of Raspberry Pi with Python
Get started with Python on macOS Big Sur
Get Gmail subject and body with Python and Gmail API
A layman wants to get started with Python
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Python script to get note information with REAPER