zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    xshell下载官网地址
    logo
    网站案例搜集
    cnblogs修改网站图标icon
    父页面调用子页面方法, 子页面加载父页面传送的数据
    对Java的常用对象(POJO、DTO、PO、BO、VO、DAO)详细解释及应用场景
    关于jsp发起请求加载datagrid数据(草稿)
    接口测试-Http状态码-postman上传文件
    httpclient获取响应实体和信息的封装方法(解耦更新)
    使用httpClient调用接口获取响应数据
  • 原文地址:https://www.cnblogs.com/liming19680104/p/13123624.html
Copyright © 2011-2022 走看看