I am using an implementation that reacts on simultaneously pressing the keys Ctrl + Alt + Shift + D meanwhile for over 2 months never having any issue. Two days ago I experienced that this short cut didn't work anymore (neither did it work on the released version, which for sure is a problem!). => Rebooting the PC eventually did its job and it worked again... ok... maybe (and hopefully) a spontaneous windows issue.
Dream on, today the same issue again and this time rebooting didn't solve it. So let's deal with it.
Here's my implementation:
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.D
&& e.Control == true
&& e.Alt == true
&& e.Shift == true)
{
// do things
}
}
As I mentioned above, it worked reliably before being used quite often.
What I discovered so far is, that pressing down the keys Ctrl + Alt + Shift one by one the "MainWindow_KeyDown" function is called each time, as expected. Pressing down the D as well this function isn't triggered anymore, which is unexpected (when only pressing D this function is called).
Removing one of the "option" keys seems to work(e.g. Ctrl + Alt + D). But as soon I have all three option keys pressed (i.e. Ctrl + Alt + Shift) it doesn't react on further keys being pressed.
Why doesn't the function "MainWindow_KeyDown" react ("anymore") on D (or any other) when Ctrl + Alt + Shift are pressed already?
A q&d workaround was to replace
e.KeyCode == Key.D
by
Keyboard.IsKeyDown(Key.D) == true
This works as long as D isn't the last key pressed (because, as described above, it won't enter this function when having pressed the keys Ctrl + Alt + Shift already).
I tried the following implementation as well with the same (negative) result:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Alt | Keys.Shift | Keys.D))
{
MessageBox.Show("Short cut found");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Any one that has experienced something similar? Thanks a lot for any input.
Form.KeyPreview
is set totrue
,ProcessCmdKey
works in any case (if (keyData.HasFlag(Keys.Control | Keys.Alt | Keys.Shift | Keys.D)) { ... }
), even when a Control uses an accelerator with similar keys. – Jimi