2010-07-14 9 views

risposta

2

MessageFilters può aiutarti in questo caso.

public class KeystrokMessageFilter : System.Windows.Forms.IMessageFilter 
    { 
     public KeystrokMessageFilter() { } 

     #region Implementation of IMessageFilter 

     public bool PreFilterMessage(ref Message m) 
     { 
      if ((m.Msg == 256 /*0x0100*/)) 
      { 
       switch (((int)m.WParam) | ((int)Control.ModifierKeys)) 
       { 
        case (int)(Keys.Control | Keys.Alt | Keys.K): 
         MessageBox.Show("You pressed ctrl + alt + k"); 
         break; 
        //This does not work. It seems you can only check single character along with CTRL and ALT. 
        //case (int)(Keys.Control | Keys.Alt | Keys.K | Keys.P): 
        // MessageBox.Show("You pressed ctrl + alt + k + p"); 
        // break; 
        case (int)(Keys.Control | Keys.C): MessageBox.Show("You pressed ctrl+c"); 
         break; 
        case (int)(Keys.Control | Keys.V): MessageBox.Show("You pressed ctrl+v"); 
         break; 
        case (int)Keys.Up: MessageBox.Show("You pressed up"); 
         break; 
       } 
      } 
      return false; 
     } 

     #endregion 


    } 

Ora nel tuo Windows NT C#, registra il MessageFilter per catturare i tratti chiave e le combinazioni.

public partial class Form1 : Form 
{ 
    KeystrokMessageFilter keyStrokeMessageFilter = new KeystrokMessageFilter(); 

    public Form1() 
    { 
     InitializeComponent(); 
    }  
    private void Form1_Load(object sender, EventArgs e) 
    { 
     Application.AddMessageFilter(keyStrokeMessageFilter); 
    } 
} 

In qualche modo rileva solo Ctrl +Alt +K. Si prega di leggere il commento nel codice MessageFilter.

8

Non so se è possibile. Ciò che si può fare, tuttavia, è il modo in cui lo fa Visual Studio. Ha scorciatoie come Ctrl + K, C. Per prima cosa stampa Ctrl +K, quindi tenere premuto Ctrl e premere C. Nel tuo caso, si potrebbe verificare la presenza di Ctrl +Alt +K, P.

È prima possibile verificare la presenza di solo Ctrl + Alt + K come avviene nelle altre risposte, quindi impostare una variabile membro/flag per indicare Ctrl + Alt + K è stato premuto. Nello stesso metodo in cui controlli K puoi controllare per P e se la bandiera che hai appena impostato era impostata su true, fai tutto ciò che devi fare. Altrimenti, riporta la bandiera su false.

ruvida pseudo-codice:

private bool m_bCtrlAltKPressed = false; 

public void KeyDown() { 
    if (Ctrl+Alt+K) 
    { 
    m_bCtrlAltKPressed = true; 
    } 
    else if (Ctrl+Alt+P && m_bCtrlAltKPressed) { 
    //do stuff 
    } 
    else { 
    m_bCtrlAltKPressed = false; 
    } 
} 

Speranza che è abbastanza chiaro.

11

È un accordo, non è possibile rilevarlo senza memorizzare dopo aver visto la prima sequenza di tasti dell'accordo. Questo funziona:

public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
    } 
    private bool prefixSeen; 

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { 
     if (prefixSeen) { 
      if (keyData == (Keys.Alt | Keys.Control | Keys.P)) { 
       MessageBox.Show("Got it!"); 
      } 
      prefixSeen = false; 
      return true; 
     } 
     if (keyData == (Keys.Alt | Keys.Control | Keys.K)) { 
      prefixSeen = true; 
      return true; 
     } 
     return base.ProcessCmdKey(ref msg, keyData); 
    } 
} 
+0

Non catturerà anche i tasti ripetuti? Ad esempio, se viene usato un accordo come CTRL + P + P? – rymdsmurf

+0

La ripetizione dei tasti produce Ctrl + P Ctrl + P. Quindi no. –

+0

Ho appena compilato e testato il codice, e _does_ (erroneamente) cattura i tasti ripetuti se l'accordo usa la stessa chiave due volte. Ad esempio, tenendo premuto CTRL + ALT + P si attiverà l'accordo CTRL + ALT + P + P. – rymdsmurf

1

È possibile utilizzare GetKeyState per ottenere lo stato degli altri tasti quando è stato premuto uno dei tasti nella combinazione. Ecco un esempio su un modulo.

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

namespace DetectKeyChord 
{ 
    public partial class Form1 : Form 
    { 
     private const int WM_KEYDOWN = 0x100; 
     private const int KEY_PRESSED = 0x80; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public void ShortcutAction() 
     { 
      MessageBox.Show("Ctrl+Alt+K+P has been pressed."); 
     } 

     private bool IsKeyDown(Keys key) 
     { 
      return (NativeMethods.GetKeyState(key) & KEY_PRESSED) == KEY_PRESSED; 
     } 

     protected override void WndProc(ref Message m) 
     { 
      if (m.Msg == WM_KEYDOWN) 
      { 
       //If any of the keys in the chord have been pressed, check to see if 
       //the entire chord is down. 
       if (new[] { Keys.P, Keys.K, Keys.ControlKey, Keys.Menu }.Contains((Keys)m.WParam)) 
       { 
        if (IsKeyDown(Keys.ControlKey) && IsKeyDown(Keys.Menu) && IsKeyDown(Keys.K) && IsKeyDown(Keys.P)) 
        { 
         this.ShortcutAction(); 
        } 
       } 
      } 
      base.WndProc(ref m); 
     } 
    } 

    public static class NativeMethods 
    { 
     [DllImport("USER32.dll")] 
     public static extern short GetKeyState(Keys nVirtKey); 
    } 
} 
Problemi correlati