1

I want to know how to use it....I want to say that if I Click Up and Right Arrow (Form1_KeyDown Event) then timer1.Start(); and When I release the Up and Right Arrow (Form1_KeyUp Event) then timer1.Stop();

I have already Imported the "User32.dll"

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(Keys ArrowKeys);

so How to use it...I see a lot of website but can't get it

Amr
  • 75
  • 1
  • 1
  • 11
  • I'm not sure what GetAsyncKeyState does but this can be done with Windows Hooks if you'd like me to explain that option – rmn36 Sep 14 '15 at 16:58
  • This can and should be solved entirely in .NET, not using external API's, by overriding the Form's key input processing. – MicroVirus Sep 14 '15 at 18:23
  • @MicroVirus I can't do Up and Right Arrow in .NET...Only Up or Only Right – Amr Sep 14 '15 at 18:28
  • @MicroVirus If you know how to enable Up and Right without using any external API's please tell me how – Amr Sep 14 '15 at 18:30
  • What do you mean by 'only up or only right'? Do you have a solution that works if the Up arrow is held? And you can't modify it to only set the timer if both are held simultaneously? – MicroVirus Sep 14 '15 at 18:31
  • @MicroVirus I can't access two arrow at the same time – Amr Sep 14 '15 at 18:41
  • Maybe this SO question can help you: [Up, Down, Left and Right arrow keys do not trigger KeyDown event](http://stackoverflow.com/questions/1646998/up-down-left-and-right-arrow-keys-do-not-trigger-keydown-event) – MicroVirus Sep 14 '15 at 20:03
  • Does this answer your question? [Check collision and prevent overlap](https://stackoverflow.com/questions/74138565/check-collision-and-prevent-overlap) – NineBerry Dec 17 '22 at 00:14

3 Answers3

0

Implement IMessageFilter for you Form and track when the Up/Right Arrows are toggled:

public partial class form1 : Form, IMessageFilter 
{

    public form1()
    {
        InitializeComponent();
        Application.AddMessageFilter(this);
    }

    private bool UpDepressed = false;
    private bool RightDepressed = false;

    private const int WM_KEYDOWN = 0x100;
    private const int WM_KEYUP = 0x101;

    public bool PreFilterMessage(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_KEYDOWN:
                if ((Keys)m.WParam == Keys.Up)
                {
                    UpDepressed = true;
                }
                else if ((Keys)m.WParam == Keys.Right)
                {
                    RightDepressed = true;
                }
                break;

            case WM_KEYUP:
                if ((Keys)m.WParam == Keys.Up)
                {
                    UpDepressed = false;
                }
                else if ((Keys)m.WParam == Keys.Right)
                {
                    RightDepressed = false;
                }
                break;
        }

        timer1.Enabled = (UpDepressed && RightDepressed);
        label1.Text = timer1.Enabled.ToString();

        return false;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        label2.Text = DateTime.Now.ToString("ffff");
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
-1

Here is how you can use it

int keystroke;

byte[] result = BitConverter.GetBytes(GetAsyncKeyState(keystroke));

if (result[0] == 1)
    Console.Writeline("the key was pressed after the previous call to GetAsyncKeyState.")

if (result[1] == 0x80)
    Console.Writeline("The key is down");
Paradise228
  • 717
  • 3
  • 16
  • The [german link](http://www.administrator.de/wissen/getasynckeystate-csharp-verwenden-151540.html) gives you all the keystrokes in integer – Paradise228 Sep 14 '15 at 17:28
  • 1
    The [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293%28v=vs.85%29.aspx) specifies not to trust the low order bit (Changed since last call). Also the key state is the high order bit ie - `(GetAsyncKeyState(keystroke) & 0x8000) == 0x8000` (or in your example `result[1] == 0x80`) The [Virtual-Key codes](https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx) are documented on MSDN, for those that don't read German. – theB Sep 14 '15 at 17:42
  • Can you explain what is this? – Amr Sep 14 '15 at 17:59
  • 2
    In .NET you can just use [Keyboard.GetKeyStates](https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.getkeystates(v=vs.110).aspx). No need for an external API call. Still, I doubt that is the actual solution to the underlying problem @Amr is having. – MicroVirus Sep 14 '15 at 18:33
  • The question was "How to use GetAsyncKeyState in C#" I just answered the question :-) – Paradise228 Sep 15 '15 at 15:42
-1

change

[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(Keys ArrowKeys);

to

[DllImport("User32.dll")]
public static extern bool GetAsyncKeyState(Keys ArrowKeys);

async key state should be a bool not a short