简体   繁体   中英

c# listening to keyboard combination

I want to write simple program that send my user and password in some places for example when I want to log in to website and i found this project that listening to keyboard.

SO i have this function:

private void HookManager_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{

}

private void HookManager_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{

}

private void HookManager_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

}

And when i press key for example a i have this pice of result:

int value = e.KeyValue; // 65
Keys key = e.KeyCode;   // A

So i wondoer how to catch specific keyboard combination and not only one key for example Ctrl + l ?

You can use code like this for KeyDown:

if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift))
{
}

Based on the KeyEventArgs documentation from MSDN:

A KeyEventArgs, which specifies the key the user pressed and whether any modifier keys (CTRL, ALT, and SHIFT) were pressed at the same time, is passed with each KeyDown or KeyUp event.

The KeyDown event occurs when the user presses any key. The KeyUp event occurs when the user releases the key. Duplicate KeyDown events occur each time the key repeats, if the key is held down, but only one KeyUp event is generated when the user releases the key.

The KeyPress event also occurs when a key is pressed. A KeyPressEventArgs is passed with each KeyPress event, and specifies the character that was composed as a result of each key press.

Overriding the ProcessDialogKey() method is the generic solution:

protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == (Keys.Control | Keys.I))
        {
            MessageBox.Show("Ctrl+I");
            return true;
        }
        return base.ProcessDialogKey(keyData);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM