Probs with GetAsyncKeyState in C#

Started by
3 comments, last by Enselic 20 years ago
I have problems with this simple inputclass. Since .NET doesnt have a GetAsyncKeyState() counterpart, I use the Win32 variant. My "inputclass" looks like this:

using System;
using System.Runtime.InteropServices;

namespace Enselic
{
	// A basic class that handles input by using the old Win32-function GetAsyncKeyState()

	public class WinInput
	{
		public static bool IsKeyDown( Keys k )
		{
			return (GetAsyncKeyState( (int)k ) & 8000) != 0;
		}

		// These values are the VK_-constants

		public enum Keys
		{
			Left = 37,
			Up = 38,
			Right = 39,
			Down = 40
		}		
		
		// Imported Win32-func

		[DllImport("User32.dll")]
		private static extern short GetAsyncKeyState( int keyCode );
	}
}
I can't get it to work properly so I wonder if any of you find any flaws in the code? Here's what MSDN says about GetAsyncKeyState() [edited by - Enselic on March 28, 2004 10:13:43 AM]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
quote:Original post by Enselic
I can''t get it to work properly

EEEP!

Try again, and include some more detail.



--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
I can't get the IsKeyDown( Keys k )-method of the class to return true when it should. And it should return true when I call WinInput.IsKeyDown( WinInput.Keys.Up ) and the Up-key is down.

[edited by - Enselic on March 28, 2004 11:45:35 AM]
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Shouldn''t that "& 8000" be "& 0x8000"? ie, its 8000 hex, not decimal. Apart from that it all looks cool.

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Argh! I feel so noobish!

Thanks mate
[s]--------------------------------------------------------[/s]chromecode.com - software with source code

This topic is closed to new replies.

Advertisement