-1

How can I catch the process that will be done when the user presses the Left-Up-Right-Down direction arrow keys respectively on the Windows Form Application respectively?

Mz.Chris
  • 1
  • 4
  • 2
    Subscribe to `KeyDown`, for the respective key? *What have you tried to code for this so far?* – gravity Jul 13 '17 at 20:37
  • I just tried to catch arrow keys with if loop (keys.up & keys.down etc.) I did research on the stackoverflow as well as on the internet. But I did not find this type of thing. – Mz.Chris Jul 13 '17 at 20:40
  • 1
    You will need to capture the keydown events and keep track of what has been pressed so far, then execute more code when the sequence is completed. Good luck and feel free to post a more specific question if you encounter a more specific problem and can share a code example. – John Wu Jul 13 '17 at 20:53
  • I tried to like this; (https://pastebin.com/qkNuCURL) In this code working smoothly, `Up+Right`. But when i change `keys = 256` to `512` and `Up+Right+Left+Up` it's not working. What can i do for resolution? – Mz.Chris Jul 13 '17 at 20:57

1 Answers1

0

One way to do it would be to track them in the KeyUp and KeyDown events. In the sample below, you can just start with a blank form, and then double-click the form to create the Form_Load event. Then add the following private members and Form_Load code, which will set the size of the form and place a label on the form:

// Fields to track keys that are pressed
private bool keyUpPressed = false;
private bool keyDownPressed = false;
private bool keyLeftPressed = false;
private bool keyRightPressed = false;

// A control to display the keypress status
private Label displayLabel = new Label();

private void Form1_Load(object sender, EventArgs e)
{
    // Set the form size and hook up the KeyDown and KeyUp events
    this.Size = new Size {Height = 700, Width = 700};
    this.KeyDown += Form1_KeyDown;
    this.KeyUp += Form1_KeyUp;

    // Put our display label on the form
    displayLabel.Location = new Point {X = 0, Y = 0};
    displayLabel.Size = new Size {Height = 100, Width = 300};
    this.Controls.Add(displayLabel);

    DisplayKeyStatus();
}

Now we can add the code for the KeyDown and KeyUp events, where we simply check which key was pressed (or un-pressed), update our private fields if necessary, and then display the key status:

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Left:
            keyLeftPressed = false;
            break;
        case Keys.Up:
            keyUpPressed = false;
            break;
        case Keys.Right:
            keyRightPressed = false;
            break;
        case Keys.Down:
            keyDownPressed = false;
            break;
    }

    DisplayKeyStatus();
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.Left:
            keyLeftPressed = true;
            break;
        case Keys.Up:
            keyUpPressed = true;
            break;
        case Keys.Right:
            keyRightPressed = true;
            break;
        case Keys.Down:
            keyDownPressed = true;
            break;
    }

    DisplayKeyStatus();
}

And finally, the method that will update our label control with the current status of the keys we're tracking:

private void DisplayKeyStatus()
{
    var message = new StringBuilder();
    message.AppendLine("Arrow keys pressed:");
    if (!(keyDownPressed || keyLeftPressed || keyRightPressed || keyUpPressed))
    {
        message.AppendLine(" - None");
    }
    else
    {
        if (keyLeftPressed) message.AppendLine(" - Left Arrow");
        if (keyUpPressed) message.AppendLine(" - Up Arrow");
        if (keyRightPressed) message.AppendLine(" - Right Arrow");
        if (keyDownPressed) message.AppendLine(" - Down Arrow");
    }

    displayLabel.Text = message.ToString();
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Firstly thank you so much for your comment, it's very helpfully for me. So how do I control this with the if loop? like this; `if (keyDownPressed && keyRightPressed && keyUpPressed) { MessageBox.Show("Test"); }` Where exactly should I use it? – Mz.Chris Jul 13 '17 at 22:22
  • I assume in the KeyDown or KeyPress event, or wherever you need to know. Is this for a game? – Rufus L Jul 13 '17 at 22:51
  • No it is not for a game. Exactly my purpose; If these keys are pressed with sequence, I want to write the process to be done. I'm trying to create a shortcut key to lock the computer. – Mz.Chris Jul 14 '17 at 09:27
  • up-to-date subject – Mz.Chris Jul 21 '17 at 10:23