ETO detect Ctrl KeyDown on button.Click event

Hi @dale, @curtisw,

I am trying to detect when a user clicked on a button while the CTRL key was pressed. In WinForms this can be done like this in the button.Click event:

def MyOnButtonClickEvent(self, sender, e):
    if sender.ModifierKeys == Keys.Control: 
        print "CTRL was down"

however in the Eto framefork i cannot find a ModifierKeys property. So i’ve set up some KeyDown and KeyUp events for the button but it seems the events are only fired for non modifier keys or if a non modifier key is pressed together with a modifier key.

How can i detect only a modifier key (CTRL) down in the button.Click event or in a button.KeyDown event ?

_
c.

Hi @clement,

To get the current modifier state for keys, such as Ctrl, Alt, and Shift, just call Eto.Forms.Keyboard.Modifiers.

– Dale

@dale, thanks. It works !

_
c.

In the keypress event, monitoring the status of a key on the keyboard seems to fail. The code doesn’t throw an error, but it doesn’t follow the logic properly. I didn’t press or release ctrl but it works

import Eto
import Eto.Forms as ef
import Eto.Drawing as ed
 
form = ef.Form()
form.Width = 100
form.Height = 100

bu_on_key = ef.Button()
bu_on_key.Text = "on_key"

def bu_on_key_mousemove(s,e):
    print("mousemove")
    if Eto.Forms.Keyboard.Modifiers.Control == Eto.Forms.Keys.Control:
        print("on_key")
    else:
        print("not_on_key")

bu_on_key.MouseMove += bu_on_key_mousemove

layout = ef.DynamicLayout()
layout.Spacing = ed.Size(1,1)
layout.AddRow(bu_on_key)
form.Content = layout
form.Show()

Hey @tom33,

Try if Eto.Forms.Keyboard.Modifiers == Eto.Forms.Keys.Control: instead.

3 Likes

Mr. Curtis Wensley, thank you

1 Like