detecting multiple key presses

26 views
Skip to first unread message

Ryan Mann

unread,
Mar 28, 2017, 10:29:53 PM3/28/17
to wxpytho...@googlegroups.com

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

 

 

Scott Talbert

unread,
Mar 28, 2017, 10:42:04 PM3/28/17
to wxpytho...@googlegroups.com
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

Ryan Mann

unread,
Mar 28, 2017, 11:41:00 PM3/28/17
to wxpytho...@googlegroups.com
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
> --
> 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-user...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marc Tompkins

unread,
Mar 29, 2017, 12:10:07 AM3/29/17
to wxpytho...@googlegroups.com
On Tue, Mar 28, 2017 at 8:40 PM, Ryan Mann <rman...@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 <s...@techie.net> wrote:
> You are creating an empty list and then appending one item to it.  :)


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:

http://stackoverflow.com/questions/10463702/multiple-key-press-detection-wxpython 

Tim Roberts

unread,
Mar 29, 2017, 1:09:07 PM3/29/17
to wxpytho...@googlegroups.com
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):


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.
-- 
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Tim Roberts

unread,
Mar 29, 2017, 1:25:53 PM3/29/17
to wxpytho...@googlegroups.com
Tim Roberts wrote:
>
> 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:

https://gist.github.com/timrprobocom/c00c0e42c4d4ed3618ed01e3dfc9657b
Reply all
Reply to author
Forward
0 new messages