Search code examples
wxpythonwxwidgets

wxWindows (Python and C++) recognizing left vs right mod keys


I'm working on a wxPython app to write out key binds for a game that can bind Left-Shift, -Ctrl, and -Alt separately from Right-Shift etc.

I can use keyEvent.ShiftDown() etc to discover whether shift is down, but apparently I need to rummage around inside keyEvent.GetRawKeyFlags() if I want info on whether it's left or right. The docs offer a little help:

Under MSW, the raw flags are just the value of lParam parameter of the corresponding message.

Under GTK, the raw flags contain the hardware_keycode field of the corresponding GDK event.

Under macOS, the raw flags contain the modifiers state.

But I'm in over my head as far as finding a nice list of the possible "lParam parameter of the corresponding message" values, etc. I found a couple lists that didn't work; I think they were probably C#-specific and/or outdated.

I figure my next step is to go traipse through the wxWidgets C++ code itself and see if I can make sense of it, or just trial-and-error 32 bits' worth of flags, but I thought I'd see if y'all had any experience with this and could point me in the right direction.

(In my code, below, I know that my modKeyFlags values are not remotely binary-flag-shaped; this part is in transition from "==" to "&" but I never found lists of flags to finish the job. You can see where this is trying to go, I hope.)

if wx.Platform == '__WXMSW__':    
    modKeyFlags = {    
        'LSHIFT': 160, 'RSHIFT': 161,    
        'LCTRL' : 162, 'RCTRL' : 163,    
        'LAlT'  : 164, 'RALT'  : 165,    
    }    
elif wx.Platform == '__WXX11_':    
    modKeyFlags = {    
        'LSHIFT': 65505, 'RSHIFT': 65506,    
        'LCTRL' : 65507, 'RCTRL' : 65508,    
        'LALT'  : 65513, 'RALT'  : 65514    
    }    
elif wx.Platform == '__WXMAC__':    
    pass    

# ... more stuff, then inside event handler:

        # check for each modifier key
        if (event.ShiftDown()) :
            if SeparateLR and modKeyFlags:
                if isinstance(event, wx.KeyEvent):
                    rawFlags = event.GetRawKeyFlags()
                    if   (rawFlags & modKeyFlags['LSHIFT']): self.ShiftText = "LSHIFT+"
                    elif (rawFlags & modKeyFlags['RSHIFT']): self.ShiftText = "RSHIFT+"
            else:
                self.ShiftText = "SHIFT+"

# ... checks for CTRL and ALT

Any info on the where to find the correct lists of key events' raw key flags for any of WXMSW, WXX11, and WXMAC would be immensely helpful. Thanks!


Solution

  • Using the "Keyboard Events" page of the wxpython demo, I can see the decimal representation of the RawFlags that the event got. Taking those decimal numbers, subtracting the smaller from the larger, and then converting the difference to binary, I get, for instance, 0b11000000000000000000 and now I just have to trial-and-error those two flags.

    Added on behalf of the question author.