Bind multiple keys to KeyDown event (Shift + * (Asterisk))

淺唱寂寞╮ 提交于 2019-12-08 09:12:18

问题


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;
    }
}


来源:https://stackoverflow.com/questions/7730699/bind-multiple-keys-to-keydown-event-shift-asterisk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!