2013-08-17 2 views

답변

10

예, "시스템 후크"라고 할 수 있습니다. Global System Hooks in .NET을 살펴보십시오.

당신이 시스템 후크에 문제가 있다면
17

음, 여기 (http://www.dreamincode.net/forums/topic/180436-global-hotkeys/ 기준) 기성품 솔루션입니다 :

public static class Constants 
{ 
    //windows message id for hotkey 
    public const int WM_HOTKEY_MSG_ID = 0x0312; 
} 

을 프로젝트에서 클래스를 정의 :

하는 프로젝트에 정적 클래스를 정의는 :

using System.Windows.Forms; 
using System.Runtime.InteropServices; 
:

public class KeyHandler 
{ 
    [DllImport("user32.dll")] 
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk); 

    [DllImport("user32.dll")] 
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id); 

    private int key; 
    private IntPtr hWnd; 
    private int id; 

    public KeyHandler(Keys key, Form form) 
    { 
     this.key = (int)key; 
     this.hWnd = form.Handle; 
     id = this.GetHashCode(); 
    } 

    public override int GetHashCode() 
    { 
     return key^hWnd.ToInt32(); 
    } 

    public bool Register() 
    { 
     return RegisterHotKey(hWnd, id, 0, key); 
    } 

    public bool Unregiser() 
    { 
     return UnregisterHotKey(hWnd, id); 
    } 
} 

은 using이 추가

private void HandleHotkey() 
{ 
     // Do stuff... 
} 

protected override void WndProc(ref Message m) 
{ 
    if (m.Msg == Constants.WM_HOTKEY_MSG_ID) 
     HandleHotkey(); 
    base.WndProc(ref m); 
} 

HandleHotkey이 버튼입니다 :

ghk = new KeyHandler(Keys.PrintScreen, this); 
ghk.Register(); 

이 양식에 그 2 가지 방법을 추가

private KeyHandler ghk; 

및 양식 생성자에서 :

지금, 당신의 양식에 필드를 추가 프레스 핸들러. 여기에 다른 매개 변수를 전달하여 버튼을 변경할 수 있습니다. ghk = new KeyHandler(Keys.PrintScreen, this);

이제 포커스가 맞지 않아도 프로그램이 buton 입력에 반응합니다.

+0

두 개의 다른 기능을 가진 두 개의 다른 단축키를 원한다면이 코드를 편집하는 방법은 무엇입니까? 어떻게 "WndProc"또는 "HandleHotkey"키를 눌렀는지 확인할 수 있습니까? – user1696947

+0

키 조합을 처리 할 수 ​​있습니까? CTRL-N과 같은 – MrVimes

0

API GetAsyncKeyState()은 Windows Hook 설정에 대한 대안으로 사용할 수 있습니다.

입력 방법에 따라 다릅니다. 이벤트 중심 알림을 선호하는 경우 갈고리를 사용하는 것이 좋습니다. 그러나 상태 변경 용 키보드를 폴링하는 경우 위의 API를 사용할 수 있습니다. 여기

GetAsyncKeyState 사용하는 방법에 대한 간단한 예제입니다 : 그것은 도움이 될처럼
Pinvoke.NET

[DllImport("User32.dll")] 
private static extern short GetAsyncKeyState(int vKey); 

private static readonly int VK_SNAPSHOT = 0x2C; //This is the print-screen key. 

//Assume the timer is setup with Interval = 16 (corresponds to ~60FPS). 
private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer(); 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    short keyState = GetAsyncKeyState(VK_SNAPSHOT); 

    //Check if the MSB is set. If so, then the key is pressed. 
    bool prntScrnIsPressed = ((keyState >> 15) & 0x0001) == 0x0001; 

    //Check if the LSB is set. If so, then the key was pressed since 
    //the last call to GetAsyncKeyState 
    bool unprocessedPress = ((keyState >> 0) & 0x0001) == 0x0001; 

    if (prntScrnIspressed) 
    { 
     //TODO Execute client code... 
    } 

    if (unprocessedPress) 
    { 
     //TODO Execute client code... 
    } 
} 
관련 문제