本文介绍了你如何检查天气或pygame上是否没有capslock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pygame程序中找到了某些代码,确切的说:

I found certain code in a pygame program, this to be exact:

caplock = False
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
       if event.key == pygame.CAPSLOCK:
          capslock = True

    if event.type == pygame.KEYUP:
       if event.key == pygame.CAPSLOCK:
          capslock = False

在关闭时按大写锁定"时,capslock

when you press "caps lock" while it is off, capslock is

True

,并且当您按下大写锁定"键时,capslock

and when you press "caps lock" while it is on, capslock is

False

代替大写开头的capslock等于False,如果已经打开大写锁定,如何使它等于True呢?如果没有关闭大写False,又如何使之等于False?

Instead of making capslock equal to False at the beginning, how can I make it equal to True if capslock is already on or False if it is already off?

推荐答案

在Linux上(在Linux Mint 18.1上经过Python 3.6测试),您可以使用:

On Linux (tested on Linux Mint 18.1 with Python 3.6) you can use:

import subprocess
if subprocess.check_output('xset q | grep LED', shell=True)[65] == 50 :
    capslock = False
if subprocess.check_output('xset q | grep LED', shell=True)[65] == 51 :
    capslock = True
print( "capslock ON is : ", capslock )

如果它不起作用,请检查subprocess.check_output()给出哪些值,如果CapsLock为ON,并且CapsLock为OFF.从理论上讲,它应该是0和1,而不是50、51,但在我的盒子上是50、51.

If it doesn't work, check which values subprocess.check_output() gives you if CapsLock is ON and if CapsLock is OFF. Theoretically it should be 0 and 1, not 50, 51, but on my box it is 50, 51.

您还可以查看"Python-如何获取当前的密钥锁状态?" 以获取更多信息.

You can also take a look at "Python - How to get current keylock status?" for further information.

是的,我想您还需要Pygame的按键事件来了解按键的状态,因此要事先知道,您必须使用Pygame之外的方法.

And YES, I think also you need a keypress event from Pygame to know the status of the keys, so to know it in advance you have to use a method from outside of Pygame.

这篇关于你如何检查天气或pygame上是否没有capslock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 11:03