How to press 3 keys at a time using KeyPress?

Is it possible to get 3 keys by time on KeyPress? I want to get Ctrl+ H+ T.,.

I tried ff:

if (e.KeyCode == Keys.H && e.Modifiers == Keys.Control)
{
    if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
    {
        Console.WriteLine("^");
    }
}

But it doesn't seem to work. I think e.KeyCode only returns one key at a time? So I'm still thinking about how I can do this ... or save the previous key to a variable? thanks in advance

+4
source share
4 answers

This should work (I tested it and seems to be doing what you need).

Ctrl + H, . , Ctrl + T, , . -, Ctrl + T, False.

private bool isCtrlHPressed;

private void txt_callerName_KeyDown(object sender, KeyEventArgs e)
{
    if (isCtrlHPressed && e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
        Console.WriteLine("^");

    isCtrlHPressed = (e.KeyCode == Keys.H && e.Modifiers == Keys.Control);
}
+2

Keys key1 = Keys.None;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (key1 == Keys.H && e.Modifiers == Keys.Control && e.KeyCode != Keys.ControlKey)
    {
        MessageBox.Show("Key Pressed");
        key1 = Keys.None;
    }
    else if (e.Control && key1 == Keys.None && e.KeyCode != Keys.ControlKey)
        key1 = e.KeyCode;
    else if (e.Control)
        key1 = Keys.None;
}
+2

use below code:

 if (e.KeyCode == Keys.X && e.Control && e.Shift) {
        // CTRL+SHIFT+X was pressed!
    }
0
source

Try using

if (e.Control && e.KeyCode == Keys.H && e.KeyCode == Keys.T)
{
    Console.WriteLine("^");
}

OR try also

if (e.Modifiers == Keys.Control && e.KeyCode == Keys.H && e.KeyCode == Keys.T) 
{
    Console.WriteLine("^");
}
0
source

Source: https://habr.com/ru/post/1547107/


All Articles