나는 GetKeyboardState 기능을 찾고 있다고 생각합니다. 한 번의 통화로 모든 가상 키의 상태를 알려 드리겠습니다. GetAsyncKeyState 함수는 사용자가 키를 눌렀는지 여부를 확인하는 데 더 유용합니다. 여기
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace s26244308
{
class Program
{
static void Main(string[] args)
{
bool keepGoing = true;
while (keepGoing)
{
int wasItPressed = SysWin32.GetAsyncKeyState(SysWin32.VK_ESCAPE);
if (wasItPressed != 0)
{
keepGoing = false; // stop
continue;
}
// for this sample: just loop a few letters
for (int x = 0x41; x <= 0x4A; x++)
{
int letterPressed = SysWin32.GetAsyncKeyState(x);
if (letterPressed != 0)
{
// now check for a few other keys
int shiftAction = SysWin32.GetAsyncKeyState(SysWin32.VK_SHIFT);
int ctrlAction = SysWin32.GetAsyncKeyState(SysWin32.VK_CONTROL);
int altAction = SysWin32.GetAsyncKeyState(SysWin32.VK_MENU);
// format my output
string letter = string.Format("Letter: {0} ({1})", Convert.ToChar(x), KeyAction(letterPressed));
string shift = string.Format("Shift: {0}", KeyAction(shiftAction));
string ctrl = string.Format("Control: {0}", KeyAction(ctrlAction));
string alt = string.Format("Alt: {0}", KeyAction(altAction));
Console.WriteLine("{0,-20}{1,-18}{2,-18}{3,-18}", letter, shift, ctrl, alt);
break;
}
}
Thread.Sleep(10);
}
Console.WriteLine("-- Press Any Key to Continue --");
Console.ReadLine();
}
private static string KeyAction(int pressed)
{
if (pressed == 0)
return "Up";
// check LSB
if (IsBitSet(pressed, 0))
return "Pressed";
// checked MSB
if (IsBitSet(pressed, 15))
return "Down";
return Convert.ToString(pressed, 2);
}
private static bool IsBitSet(int b, int pos)
{
return (b & (1 << pos)) != 0;
}
}
class SysWin32
{
public const int VK_ESCAPE = 0x1B;
public const int VK_SHIFT = 0x10;
public const int VK_CONTROL = 0x11;
public const int VK_MENU = 0x12;
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Int32 i);
}
}
콘솔 출력 :
당신이 ['GetAsyncKeyState'에 대한 설명서 읽기]를 했습니까 (HTTP 연습으로
, 나는하여 GetAsyncKeyState API 호출을 입증하기 위해 다음 썼다 : //msdn.microsoft.com/en-us/library/windows/desktop/ms646293 (v = vs.85) .aspx)? –예 uwe 나는 그것을 읽었지 만 나 같은 괴상한 것이었다. 실제로 win32 API는 나에게 무서워 보인다. 나는 그것을 반복해서 읽고 쓰레기에 코드를 던지기로 결정했다. 그런 다음 나는 stackoverflow가 있다고 생각했다. 도와주세요. –
@khan, 그러면 설명서를 이해하는 데 필요한 배경 지식과 기술을 개발해야합니다. 그렇지 않으면 실제 코드가하는 것을 이해할 기회가 없습니다. –