c# - Understanding multiple keys with WPF KeyDown event -
i'm using wpf keydown
event. can please explain why condition true, when press ctrl+f1? when press f1, ctrl pressed !keyboard.iskeydown(key.leftctrl)
should false.
edit:
in code below if press ctrl+f1 both messages fire. if change order of these 2 if statements, "ctrlf1" message fire should be. explanation of strange behavior.
private void window_keydown(object sender, keyeventargs e) { if (e.key == key.f1 && keyboard.iskeydown(key.leftctrl)) { messagebox.show("ctrlf1"); } if (e.key == key.f1 && !keyboard.iskeydown(key.leftctrl)) { messagebox.show("f1"); } }
the difference follows:
- in code showed, when entering handler, f1 pressed , ctrl pressed (both conditions of first if-clause true). messagebox blocks thread. meanwhile release ctrl key , click @ message. code execution goes on , ctrl key no longer pressed (both conditions of second if-clause true)
- if switch if-statements, first condition (
e.key == key.f1
) of first if-statement true. execution comes second if-statement , both conditions true. messagebox shown , execution stops till messagebox closed.
the difference is: pressing of f1 key evaluated before handler called, check keyboard.iskeydown(key.leftctrl)
evaluated in moment, when line of code executed
Comments
Post a Comment