2013-08-17 4 views
11

मैं Print Screen बटन प्रेस का पता लगाने की कोशिश कर रहा हूं, जबकि फॉर्म वर्तमान सक्रिय एप्लिकेशन नहीं है।ध्यान केंद्रित करते समय मैं कुंजीपटल का पता कैसे लगा सकता हूं?

यदि संभव हो तो ऐसा कैसे करें?

+0

से व्युत्पन्न है जैसे कि यह मदद कर सकता है लगता है: http://social.msdn.microsoft.com/Forums/ en-US/94b1598f-79ce-4c70-8851-86aff6d9b12f/कैप्चरिंग-द-प्रिंट-स्क्रीन-कुंजी – Surfbutler

उत्तर

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); 
    } 
} 

usings जोड़ने

private KeyHandler ghk; 

और फार्म निर्माता में:

अब, अपने फार्म में, फ़ील्ड जोड़

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

अपने फ़ॉर्म के उन 2 तरीकों जोड़ें:

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);

अब आपका प्रोग्राम फ़ोकस इनपुट के लिए प्रतिक्रिया करता है भले ही ध्यान केंद्रित न हो।

+0

इस कोड को कैसे संपादित करें, अगर मैं दो अलग-अलग कार्यक्षमताओं के साथ 2 अलग हॉटकी चाहता हूं? क्या मैं किसी भी तरह से "WndProc" या "HandleHotkey" में जांच सकता हूं जिसे कुंजी दबाया गया था? – user1696947

+0

क्या यह कुंजी संयोजनों को संभालने के लिए अनुकूलित किया जा सकता है? जैसे कि CTRL-N – MrVimes

0

एपीआई GetAsyncKeyState() विंडोज हुक स्थापित करने के लिए एक पूरी तरह स्वीकार्य विकल्प हो सकता है।

यह इस बात पर निर्भर करता है कि आप इनपुट प्राप्त करने की इच्छा कैसे रखते हैं। यदि आप घटना-संचालित अधिसूचनाओं को प्राथमिकता देते हैं, तो एक हुक जाने का रास्ता है; हालांकि, अगर आप मतदान राज्य परिवर्तनों के लिए कीबोर्ड पसंद करते हैं, तो आप ऊपर दिए गए एपीआई का उपयोग कर सकते हैं।

यहाँ 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... 
    } 
} 
संबंधित मुद्दे

 संबंधित मुद्दे