|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FormExNS
{
public enum KeyState { Up = 0, Down = 1 }
public enum KeyValue { Untoggled = 0, Toggled = 1 }
internal static class KeyStateCheck
{
//GetKeyState Win32 API declaration
[DllImport("user32.dll")]
private static extern short GetKeyState(int nVirtKey);
private static short m_KeyDown = Convert.ToInt16("1000000000000000", 2); //High-Order bit set (KeyDown)
private static short m_KeyUp = Convert.ToInt16("0000000000000000", 2); //High-Order bit not set (Key Up)
private static short m_KeyToggled = Convert.ToInt16("0000000000000001", 2); //Low-Order bit set (Key Toggled)
private static short m_KeyUnToggled = Convert.ToInt16("0000000000000000", 2); // Low-Order bit not set (Key Untoggled)
// Get if Key is Up or Down
public static KeyState GetKeyState(Keys virtualKey)
{
short keyState = GetKeyState((int)virtualKey);
KeyState state;
// Bitwise AND to get wether the key is Down
if ((keyState & m_KeyDown) == m_KeyDown)
state = KeyState.Down;
else
state = KeyState.Up;
return state;
}
// Get if key is toggled or untgled (useful to detect if capslock or nunlock is on)
public static KeyValue GetToggled(Keys virtualKey)
{
short keyState = GetKeyState((int)virtualKey);
KeyValue value;
// Bitwise AND to get wether the key is toggled
if ((keyState & m_KeyToggled) == m_KeyToggled)
value = KeyValue.Toggled;
else
value = KeyValue.Untoggled;
return value;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Fábio is a programmer since 14 years, which start with his curiosity in games and how to make them.
After that his taste for programming only grew, at the age of 16 Fábio accidentaly stumbled in a program that makes programs, a.k.a. Visual Basic 5.
From there he self taught, through books, how to code in VB 5, 6 and later C, C++ and finally C#.
Currently Fábio specializes in C# Desktop and Web Development, that he's been working with since 2004. When he has time, he helps users on MSDN forums and tries to write articles on codeproject.
MSDN Profile: http://social.msdn.microsoft.com/profile/f%C3%A1bio%20franco/?type=forum