2012-01-22 2 views
0

Windows 7 x64에서 C#으로 GetAsyncKeyState (i)를 사용하여 키를 가져 오려고합니다. x86에서 완벽하게 작동합니다. 내 코드는 다음과 같습니다.Windows 7 x64에서 GetAsyncKeyState

[DllImport("user32.dll")] 
public static extern int GetAsyncKeyState(long vKey); 
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)] 
public static extern short GetKeyState(int keyCode); 

     search = false; 
     int key_my; 
     for (i = 0; i < 255; i++) 
     { 
      key_my = GetAsyncKeyState(i); // this should return -3.... but it does 46........... 
      if (key_my == (System.Int16.MinValue + 1)) 
      { search = true; break; } 
     } 
     if (search == true) 
     { 
      ...//using if to keys here. 
     } 

IDEA?

+0

VB6 선언입니다. 올바른 것을 찾으려면 pinvoke.net을 사용하십시오. –

답변

5

나는 지적하고 싶습니다의 GetAsyncKeyState 기능은 short하지 int의 반환 형식을 가져야하며이 질문에 이미 정답되어 있지만 매개 변수가 int하지 long

[DllImport("user32.dll")] 
public static extern short GetAsyncKeyState(int vKey); 
+0

완벽하게 작동합니다! 조언 해 주셔서 감사합니다. –

0

에 입력해야합니다입니다 내 이 대답을 사용한 경험과 그것을 한 줄에 넣으면 이 아닌이 작동합니다.

 if (GetAsyncKeyState(27) == (System.Int16.MinValue + 1)) 

하지 작업을 수행합니다.

   int key_my = GetAsyncKeyState(27); 
       if (key_my == (System.Int16.MinValue + 1)) 

작업을 수행합니다.

(이유를 모르겠다.)

관련 문제