Archived

This topic is now archived and is closed to further replies.

Enselic

Probs with GetAsyncKeyState in C#

Recommended Posts

Enselic    829
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]

Share this post


Link to post
Share on other sites
Enselic    829
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]

Share this post


Link to post
Share on other sites