如何对双键按下进行编程,以使非修饰键的行为像程序中的修饰键一样? [英] How do I program a double key press to make a non-modifier key behave like a modifier key within my program?

查看:120
本文介绍了如何对双键按下进行编程,以使非修饰键的行为像程序中的修饰键一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个打字程序,其中包含的字符比标准键盘上可用的字符多得多.为了实现这一点,我需要将一些字母键转换为修饰键 CTRL + A .例如, f + j 将输出 a .输入 f ,然后 j 对用户来说很慢,我需要他们能够在以下位置按 f j 同时接收一个输出.如果在程序运行时停止了某些键盘的正常功能,那就很好(甚至更好).

I am writing a typing program that includes many more characters than are available on a standard keyboard. In order to achieve this I need to transform some of the alphabet keys into modifier keys CTRL+A. For example f+j would output a. Typing f then j is slow for the user, I need them to be able to press f and j at the same time and receive one output. It's fine (preferable even) if some of the keyboard's normal functionality is stopped while the program is running.

我研究了 pygame Keydown ,但它似乎仅具有增加按键重复次数而不停止按键输出的功能.也可以使用 Pyglet ,但是它没有关于如何制作其他修饰键的确切文档.我能确定的唯一方法是不断扫描整个键盘以查看是否有按键被按下,但这并不能确定按键的按入顺序,并且会给用户造成错误,因为用户在按下f时会出现错误. j的读取方式与用户按j然后按f的方式相同,而我只需要按f然后j的组合键就可以理解为系统的击键.

I have looked into pygame Keydown, but it only seems to have functions for increasing key repeat and not stopping key output. Pyglet is also a possibility, but it doesn't have exact documentation on how I could make additional modifier keys. The only way I can figure out is to be constantly scanning the whole keyboard to see if any keys are pressed, but that won't determine the order the keys are pressed in and will create errors for the user, as the user pressing f then j would be read the same as the user pressing j then f and I need only the f then j combo to be understood as a keystroke by the system.

推荐答案

下面是一些简单的代码,用于打印快速连续按下的键,这些都是用Python 2写的.应该可以轻松地对其进行修改以满足您的需求:

Here is some simple code to print keys pressed in quick succession, written in Python 2. It should be able to easily be modified to suit your needs:

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([500,500])
clock = pygame.time.Clock()

combokeys = []
timer = 0
ACCEPTABLE_DELAY = 30 #0.5 seconds

while 1:
    clock.tick(60)
    timer += 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if timer <= ACCEPTABLE_DELAY:
                combokeys.append(event.unicode)
            else:
                combokeys = [event.unicode]
            timer = 0
            print combokeys

我无法测试此代码(在学校计算机上工作),因此如果某些操作无效,请在评论中通知我,以便我进行修复.

I have not been able to test this code (working at school computer), so please notify me in the comments if something did not work so I can fix it.

您可以更改为ACCEPTABLE_DELAY给出的值,以更改延迟,然后再将某些东西视为其他按键组合.延迟应为(ACCEPTABLE_DELAY/60)秒.

You can change the value given for ACCEPTABLE_DELAY to change the delay before something is considered a different key combination. The delay should be (ACCEPTABLE_DELAY/60) seconds.

这篇关于如何对双键按下进行编程,以使非修饰键的行为像程序中的修饰键一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆