[英]How to get correct logic truth table results with two inputs in pygame without having to press both inputs at exactly the right time
I'm planning on making a keyboard that uses only 5 keys by using different combinations of switch presses but there's one thing I can't figure out how to resolve so I want to try and recreate a basic example of the board in python using keyboard presses to simulate the buttons which would have been in real life.我打算通过使用不同的按键组合来制作一个仅使用 5 个键的键盘,但有一件事我不知道如何解决,所以我想尝试使用键盘在 python 中重新创建一个基本的板示例按下以模拟现实生活中的按钮。
Say I had a red button and a blue button.假设我有一个红色按钮和一个蓝色按钮。 When I press only the red button I should hypothetically get an output of "A".当我只按下红色按钮时,假设我应该得到一个“A”的 output。 When I press only the blue button I should get an output of "B" and when I press both at the same time, I should get an output of "C".当我只按下蓝色按钮时,我应该得到一个“B”的 output,当我同时按下两个按钮时,我应该得到一个“C”的 output。
The problem is I would have to press the red and blue button at exactly the same time for the computer to understand that I want the output "C" and if I press one before the other, I get two characters outputted rather than one.问题是我必须同时按下红色和蓝色按钮,计算机才能理解我想要 output “C”,如果我在另一个之前按下一个,我会输出两个字符而不是一个。
I haven't really written any code for it but I would like someone to help me solve this problem so that I can implement it into my PyGame program and then onto an Arduino later on.我还没有真正为它编写任何代码,但我希望有人帮我解决这个问题,这样我就可以将它实现到我的 PyGame 程序中,然后再实现到 Arduino 程序中。
Probably something quick like a dictionary-based lookup table would work.可能像基于字典的查找表这样的快速方法会起作用。 On the Arduino I would use a 2-byte lookup table in a speed+space efficient data structure though.不过,在 Arduino 上,我会在速度+空间高效的数据结构中使用 2 字节查找表。
### Chord key mapping table
key_table = { "a" : "a",
"s" : "e",
"d" : "i",
"f" : "o",
"g" : "u",
"as" : "s",
"ad" : "t",
"af" : "n" }
In the main loop build up the map-key by looking at the pygame.key.get_pressed()
tuple:在主循环中,通过查看pygame.key.get_pressed()
元组来构建映射键:
keys = pygame.key.get_pressed()
# print( str( keys ) )
chord_pressed = ''
if ( keys[pygame.K_a] ): # note: the order the chord is made
chord_pressed += 'a' # is significant
if ( keys[pygame.K_s] ):
chord_pressed += 's'
if ( keys[pygame.K_d] ):
chord_pressed += 'd'
if ( keys[pygame.K_f] ):
chord_pressed += 'f'
if ( keys[pygame.K_g] ):
chord_pressed += 'g'
if ( chord_pressed in key_table.keys() ):
print( "[%s] → [%s]" % ( chord_pressed, key_table[ chord_pressed ] ) )
Seems to work OK:似乎工作正常:
[a] → [a] [一] → [一]
[a] → [a] [一] → [一]
[as] → [s] [as] → [s]
[as] → [s] [as] → [s]
[as] → [s] [as] → [s]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.