5

In my application, I would like to check for a GTK key_press event with the key combination <Ctrl><Shift><#>.
My code: (In this case, Python, but the problem is language-independent, I guess)

def on_key_press(self, widget, event):
    if ((event.keyval == Gdk.KEY_numbersign) \  # The problem
    and (event.state & Gdk.ModifierType.SHIFT_MASK) \
    and (event.state & Gdk.ModifierType.CONTROL_MASK)):

        dosomethingamazing()

The way Gtk handles keypresses, the actual key (for example "a") and the shift key are combined to an uppercase A. So numbersign keys get replaced by an apostrophe key on the QWERTZ layout when the shift key is pressed. The event.keyval for this combination is 39 (apostrophe), not 35 (numbersign). So, for <Ctrl><Shift><#> I need to check for the uppercase equivalent of numbersign, instead of numbersign, which might be different on different keyboard layouts, so I cannot hard-code it.

The problem is the following:

>>> Gdk.keyval_to_upper(35)  # 35 is the Key Value for numbersign
35
>>> Gdk.keyval_is_upper(35)
True
>>> Gdk.keyval_is_lower(35)
True

I cannot get the uppercase equivalent using these functions. Any ideas on how to fetch the result of a <shift><whateverkey> combination, where keyval_to_upper() fails?

  • Why not choose a key that is not ambiguous? – ntd Jul 06 '15 at 16:55
  • @ntd because in my case, it just makes sense: it is a shortcut to toggle headings in markdown syntax, which are masked with numbersigns –  Jul 06 '15 at 17:29
  • 1
    But I worked around and used ctrl-alt-numbersign instead of shift –  Jul 06 '15 at 17:30
  • That's much better in my opinion (at least in terms of ambiguity). Keep in mind on my keyboard I must press four keys to get that shortcut (I have to press Alt Gr to get #). – ntd Jul 06 '15 at 17:59
  • Thanks for pointing that out. I use a German qwertz keyboard, I did not really look at other keyboard layouts –  Jul 06 '15 at 18:03

0 Answers0