For a project I will be working on, I am trying to emulate a Braille keyboard. To do this, I need to see if more than one key is pressed at a time then put the keys that were pressed into a list. For experimentation, I have the below code. It seems to only put one key into the list.
You are creating an empty list and then appending one item to it.
Did you mean to make keylist an instance variable or something?
Scott
···
On Tue, 28 Mar 2017, Ryan Mann wrote:
For a project I will be working on, I am trying to emulate a Braille
keyboard. To do this, I need to see if more than one key is pressed at a
time then put the keys that were pressed into a list. For
experimentation, I have the below code. It seems to only put one key into
the list.
def onCharEvent\(self, event\):
keycode = event\.GetKeyCode\(\)
keylist=\[\]
keylist\.append\(keycode\)
print keycode
event\.Skip\(\)
print "You pressed the following keys:"
for i in keylist:
print i
I figured I was making an empty list, but I couldn't use the append method unless I made an empty list first.
···
Sent from my iPhone
On Mar 28, 2017, at 10:42 PM, Scott Talbert <swt@techie.net> wrote:
On Tue, 28 Mar 2017, Ryan Mann wrote:
For a project I will be working on, I am trying to emulate a Braille
keyboard. To do this, I need to see if more than one key is pressed at a
time then put the keys that were pressed into a list. For
experimentation, I have the below code. It seems to only put one key into
the list.
def onCharEvent(self, event):
keycode = event.GetKeyCode()
keylist=
keylist.append(keycode)
print keycode
event.Skip()
print "You pressed the following keys:"
for i in keylist:
print i
You are creating an empty list and then appending one item to it.
Did you mean to make keylist an instance variable or something?
Scott
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I think he meant to emphasize the second part of that statement:
event.GetKeyCode() will only return one value at a time, and you're only
catching it once, so...
In general, there don't seem to be any off-the-rack methods for catching
multiple simultaneous keypresses ("chords", I think they're called?).
You'll need to roll your own, I think; the second answer from this
StackOverflow question might be helpful:
···
On Tue, Mar 28, 2017 at 8:40 PM, Ryan Mann <rmann0581@gmail.com> wrote:
I figured I was making an empty list, but I couldn't use the append method
unless I made an empty list first.
> On Mar 28, 2017, at 10:42 PM, Scott Talbert <swt@techie.net> wrote:
> You are creating an empty list and then appending one item to it.
The EVT_CHAR event is clearly the wrong mechanism. That only gets
sent when a key is pressed. There’s no way to know what the other
keys are doing.
Let’s step back and think about your problem a bit. In the PC
world, keys are never literally pressed simultaneously. Key events
are recorded sequentially, as a series of “key down” events and “key
up” events. Even if you do physically close the contacts at exactly
the same time, keyboards contain arbitration circuitry to decide
which key event will be sent first. You can use the EVT_KEY_DOWN
and EVT_KEY_UP events to capture those events and keep your own
list. By doing that, at any given time you can know which keys are
currently depressed.
However, there’s another design problem here. As a Braille
simulator, you need to know when a chord is finished. If someone
pressed S, D, J, and K and intended it to be a chord, you’ll see:
S key down (now my list contains S)
D key down (now my list contains S, D)
J key down (now my list contains S, D, J)
K key down (now my list contains S, D, J, K)
But how do I know when it’s finished? To do that, you’re going to
have to institute some kind of timeout. When you get the first
keystroke in an empty list, you can start a 50ms timer (for
example). When the timer expires, you assume that your list
contains the completed chord. You might also need to handle the
case of a person releasing the chord quickly; if you get a key-up
event and your timer hasn’t expired yet, you can assume the list
(before the key-up event) is a chord.
I don’t know how fast Braille typists usually go, but this should be
doable.
···
Ryan Mann wrote:
For a project I will be working on, I am trying to emulate a
Braille keyboard. To do this, I need to see if more than one
key is pressed at a time then put the keys that were pressed
into a list. For experimentation, I have the below code. It
seems to only put one key into the list.
But how do I know when it's finished? To do that, you're going to
have to institute some kind of timeout. When you get the first
keystroke in an empty list, you can start a 50ms timer (for example).
When the timer expires, you assume that your list contains the
completed chord. You might also need to handle the case of a person
releasing the chord quickly; if you get a key-up event and your timer
hasn't expired yet, you can assume the list (before the key-up event)
is a chord.
Here is a simple sample demonstrating this technique: