Bind multiple keys to KeyDown event (Shift + * (As

2019-06-10 03:03发布

问题:

I am trying to bind multiple keys on a KeyDown event to change a bool variable, but I can't seem to figure out how to trigger the asterisk/star key (*) with the Left Shift key in the following code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Multiply || keyData == (Keys.LShiftKey | Keys.OemQuotes))
    {
        Valgt = true;
    }
}

回答1:

This answer will not be keyboard layout invariant but this would do the trick on a US-EN keyboard. It's not robust but can be adapted to your local layout.

if (keyData == Keys.Multiply || keyData == (Keys.Shift | Keys.D8))
{
    Valgt = true;
}

Alternatively you can use Control_KeyPress event

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{            
    if (e.KeyChar == '*')
    {
        Valgt = true;
    }
}