1

I know that I can get the ctl/alt key states from the keypress event when another key is pressed. But I can't find any way to detect the key state when, say, a mouse button is clicked. Yes, I get events when the ctl/alt keys are pressed and released, and I could keep track of whether they are up or down AS LONG AS the window stays in focus. But if the user, say, clicks on the desktop to take my window out of focus, then presses and holds the ctl or alt key down, and then clicks on my window, my code won't have seen the ctl key down event, and won't know it's down.

Is there a way to check the state of such modifier keys when a mouse click, or some other non-keyboard event, occurs?

tgphelps
  • 47
  • 1
  • 4

1 Answers1

1

Instead of trying to keep a state around permanently, check for modifiers every time you handle an event.

Examples:

https://developer.gnome.org/gtk3/stable/checklist-modifiers.html

Detect ctrl+click on button in pygtk

theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • OH. I hadn't read about get_state(), and wouldn't have thought to call it on a mouse event anyhow, since it's retrieving keyboard information. Looks like a correct answer, but I haven't verified yet. – tgphelps Sep 04 '20 at 13:26
  • Question: Your pygtk example calls get_state(), but the other article seems to say to always call gtk_accelerator_get_default_mod_mask() to get the modifiers. Those seem to be mutually exclusive. Maybe the pygtk code was just to show me a code example (which wasn't doing it exactly right), and the page from the GTK docs shows The Right Way to do it. Is that true? Please clarify that for me. – tgphelps Sep 04 '20 at 13:44
  • I noticed that myself, but didn't get to dig into it and figure out the difference, or which way is best. – theGtknerd Sep 04 '20 at 16:27
  • I didn't say that right. Both URLs use the event state. The first ANDs it with the result of calling a function, and the other ANDs it with various GDK_*_MASK constants. For my purposes, I can't see what the first methods adds. I've done some testing, and I get the same answer either way. In any case, I'm going to stop here, and mark this question as ANSWERED. – tgphelps Sep 08 '20 at 19:06