Python 讀取鍵盤輸入字元

2021-04-21 08:03:28 字數 433 閱讀 7157

找了一圈,發現python下讀取鍵盤輸入的字元還挺麻煩的,找到這個例子,linux下用這個,ch是讀取的字元

import  os

import  sys

import  tty, termios

fd = sys.stdin.fileno()

old_settings = termios.tcgetattr(fd)

try :

tty.setraw( fd )

ch = sys.stdin.read( 1 )

finally :

termios.tcsetattr(fd, termios.tcsadrain, old_settings)

在windows下,就簡單的多

import  msvcrt

ch = msvcrt.getch()

Python讀取鍵盤輸入

python提供了兩個內建函式從標準輸入讀入一行文字,預設的標準輸入是鍵盤。如下 raw input 函式從標準輸入讀取乙個行,並返回乙個字串 去掉結尾的換行符 str raw input enter your input print received input is str 這將提示你輸入任意字...

python 鍵盤輸入

python鍵盤輸入與其他程式語言基本類似,回車鍵結束輸入 下面來看一段鍵盤輸入年月日,英文輸出示例 1 usr bin env python2 coding utf 8 3 定義英文月份 4 months january february march april may june july aug...

Python模擬鍵盤輸入

2.程式實現 import win32api import win32con win32api.keybd event 17,0,0,0 ctrl鍵位碼是17 win32api.keybd event 86,0,0,0 v鍵位碼是86 win32api.keybd event 86,0,win32c...