Windows Develop Bookmark and Share   
 index > Windows Forms General > getasynckeystate() example
 

getasynckeystate() example

i'm an absolute beginner at c# and i need to make a form that recognises if w a s d is pressd. a portion of the code is this

  [DllImport("user32.dll")]
  internal static extern ushort GetAsyncKeyState(int vKey);
  static void Main(string[] args)
{       ushort down = 32768;
         ushort up=32768;
 while (true)
         { if ((GetAsyncKeyState(0x26)&up)==up)
               MessageBox.Show("up pressed");
               else 
                 //something
           if ((GetAsyncKeyState(0x28)&down)==down)
                //something
               else 
                //something

i managed to make it work (after days) butin console only. can you help me put it in a windows form?
the internet wasn't very helpful, there are a lot of getasynckey examples in vb (even on youtube) and the few that i found in c# were very complicated or gave errors when i tried to put them in quicksharp or c# - i should have modified something to work, but i don't know.
so, please, help me put it in a windows form; just a empty form and when w or up key (whatever suits you) is pressed, a dialog appears, or something.

Vandal S  Saturday, July 04, 2009 1:24 PM
Using GetAsyncKeyState() requires polling. Which requires a "game loop". You can make one by making your Program.cs file look like this:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 {
class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Program().RunGameLoop();
}

Form1 mainForm;

void RunGameLoop() {
mainForm = new Form1();
mainForm.FormClosed += new FormClosedEventHandler(fmain_FormClosed);
mainForm.Show();
while (!mQuit) {
System.Threading.Thread.Sleep(1);
pollKeyboard();
Application.DoEvents();
}
}

private bool mQuit;

void fmain_FormClosed(object sender, FormClosedEventArgs e) {
mQuit = true;
}

void pollKeyboard() {
// You'll probably want to call, say, formMain.MoveLeft() here...
if (GetAsyncKeyState(Keys.S) < 0) Console.WriteLine("moving left");
// etc...
}

[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(Keys key);

}
}


Hans Passant.
nobugz  Saturday, July 04, 2009 1:42 PM
To clarify, are you trying to determine if W, A, S, D are all held down simultaneously?

Trying to figure out why you think you need to use GetAsyncKeyState instead of another method.
BinaryCoder  Saturday, July 04, 2009 1:35 PM
Using GetAsyncKeyState() requires polling. Which requires a "game loop". You can make one by making your Program.cs file look like this:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1 {
class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new Program().RunGameLoop();
}

Form1 mainForm;

void RunGameLoop() {
mainForm = new Form1();
mainForm.FormClosed += new FormClosedEventHandler(fmain_FormClosed);
mainForm.Show();
while (!mQuit) {
System.Threading.Thread.Sleep(1);
pollKeyboard();
Application.DoEvents();
}
}

private bool mQuit;

void fmain_FormClosed(object sender, FormClosedEventArgs e) {
mQuit = true;
}

void pollKeyboard() {
// You'll probably want to call, say, formMain.MoveLeft() here...
if (GetAsyncKeyState(Keys.S) < 0) Console.WriteLine("moving left");
// etc...
}

[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(Keys key);

}
}


Hans Passant.
nobugz  Saturday, July 04, 2009 1:42 PM
Hi Vandal,

You can set the KeyPreview property of the Form to true and subscribe the KeyDown event of the Form to get notified when a key is pressed. For example:

public class Form1:Form
{
public Form1()
{
this.KeyPreview = false;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.keyCode.ToString() + " is pressed");
}
}

Hope this helps.
Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Linda Liu  Monday, July 13, 2009 9:10 AM

You can use google to search for other answers

Custom Search

More Threads

• change Form RightToLeft property at runtime
• System.ArgumentException: Parameter is not valid. at System.Drawing.Graphics.GetHdc()
• Problem with databinding
• How to make checkboxes in corresponds with the listview
• Vertical ScrollBar for flowlayoutpanel, to scroll pictureBox control in it.
• Invoke a controls property
• RichTextBox Selection Back Color Hovering?
• Context Menu Strip
• In TreeView treenode BeginEdit() is not working
• Textbox Initialization