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

  • 相关阅读:
    mac快捷键,pycharm快捷键
    Django进阶之session
    Python:如何将字符串作为变量名
    Ubuntu中创建用户
    redis在centos上的安装
    centos--网络配置问题,提示connect: Network is unreachable
    Python 3.x--paramiko模块详解
    Python 3.x--paramiko模块安装过程中的错误
    Python 3.x--Socket实现简单的ssh和文件下载功能
    Python 3.x--面向对象编程(二)静态方法、类方法、属性方法
  • 原文地址:https://www.cnblogs.com/liming19680104/p/13123624.html
Copyright © 2011-2022 走看看