pygame.key
import pygame pygame.init() screen = pygame.display.set_mode((196, 60)) pygame.display.set_caption("pygame.key") pygame.event.set_grab(False) #是否独占所有的输入设备 #设为True时,鼠标不能移开窗口的用户区,此时不能通过鼠标操作别的程序了,但是可以通过ALT+TAB切换,也可CTRL+ALT+DEL x=pygame.event.get_grab() #返回pygame.event.set_grab函数设置的状态 print(x) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() b = pygame.key.get_focused() # 当窗口获得焦点时返回 True pygame.display.update()
import pygame pygame.init() screen = pygame.display.set_mode((196, 60)) pygame.display.set_caption("pygame.key") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() b = pygame.key.get_pressed() # 获取键盘上所有按键的状态 #返回一个由布尔类型值组成的序列,表示键盘上所有按键的当前状态。使用 key常量作为索引,如果该元素是 True,表示该按键被按下 ''' (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) 以上是CTRL键和a键按下时返回的值 ''' if b[pygame.K_a]==True: print('你按下了a键') if b[pygame.K_LCTRL]==True: print('你按下了左CTRL键') if b[pygame.K_c]==True and b[pygame.K_LCTRL]==True: print('你同时按下了左CTRL键+c键') pygame.display.update()
import pygame pygame.init() screen = pygame.display.set_mode((196, 60)) pygame.display.set_caption("pygame.key") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() mods=pygame.key.get_mods() #返回组合键的掩码 if mods & pygame.KMOD_CTRL : print('你按下了CTRL键') if mods & pygame.KMOD_CTRL and mods &pygame.KMOD_SHIFT : print('同时按下了CTRL键+Shift键') pygame.display.update()
pygame.key.set_mods() 临时设置某些组合键为被按下状态
比如我们需要设置 ctrl 和 alt 组合键为按下状态,则可以 mods = KMOD_CTRL | KMOD_ALT,然后调用 pygame.key.set_mods(mods),这样尽管用户没有按下 ctrl 和 alt 组合键,它们依然是显示被按下状态
import pygame pygame.init() screen = pygame.display.set_mode((196, 60)) pygame.display.set_caption("pygame.key") pygame.key.set_repeat(500,500) #控制重复响应持续按下按键的时间 #当开启重复响应按键,那么用户持续按下某一按键,就会不断产生同一 pygame.KEYDOWN 事件。 # 参数1设置多久后(单位是毫秒)开始发送第一个 pygame.KEYDOWN 事件。参数2设置发送两个事件之间的间隔。如果不传入任何参数,表示取消重复响应按键 i=0 while True: event = pygame.event.wait() if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: print(i,'向左键') x=pygame.key.get_repeat() #获取pygame.key.set_repeat设置的两个参数值 print(x) i=i+1
import pygame pygame.init() screen = pygame.display.set_mode((196, 60)) pygame.display.set_caption("pygame.key") str=pygame.key.name(pygame.K_a) #获取按键标识符对应的名字 #返回值string类型---a i=0 while True: event = pygame.event.wait() if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: x=pygame.key.name(event.key) #获取按键标识符对应的名字 print(i,x) i+=1
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
2019-06-14 pyqt5-橡皮筋控件QRubberBand