2013-08-16 16 views
8

Da qualche tempo sto cercando di recuperare Ctrl +Alt +spostamento a destra chiave in comune VBNET di gestione dei tasti. Ecco le mie prove:Come catturare Ctrl + Alt + RShftKey

If e.Control And e.Alt And e.KeyCode = Keys.Space Then 
     MsgBox("CTRL + ALT + SPACE") ' This work 
    End If 

    If e.Control And e.Shift And e.KeyCode = Keys.F10 Then 
     MsgBox("CTRL + SHIFT + F10") ' This work 
    End If 

    If e.Control And e.Alt And e.KeyCode = Keys.ShiftKey Then 
     MsgBox("CTRL + ALT + SHIFT") ' This work 
    End If 

    If e.Alt And e.Shift And e.KeyCode = Keys.LWin Then 
     MsgBox("ALT + SHIFT + LEFT WINDOWS") ' This work 
    End If 

    If e.Control And e.Alt And e.KeyCode = Keys.RShiftKey Then 
     MsgBox("CTRL + ALT + RIGHT SHIFT") ' This don't work 
    End If 

di Windows 7, WinForms, VB2008, .NET framework 2.0

Perché non posso prendere Ctrl +Alt +spostamento a destra chiave nella situazione descritta?
Oppure, come faccio prendo Ctrl +Alt +spostamento a destra chiave combinazione?

risposta

6

Non esiste alcun modo per rilevare la differenza tra turni utilizzando l'approccio VB.NET standard. Si dovrà collegare in API di Windows per questo:

<System.Runtime.InteropServices.DllImport("user32.dll")> _ 
    Private Shared Function GetAsyncKeyState(vKey As Keys) As Short 
    End Function 

    Private Sub Form2_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown 

     If e.Control And e.Alt And e.Shift Then 

      If Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)) Then 
       MsgBox("CTRL + ALT + LEFT SHIFT") 
      ElseIf Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)) Then 
       MsgBox("CTRL + ALT + RIGHT SHIFT") 
      End If 

     End If 

    End Sub 
+0

Molto interessante. Perché allora RShiftKey e LShiftKey sono enumerati sotto le costanti di Keys? Lo stesso valeva per LControlKey e RControlKey? Cos'altro non funziona come previsto? –

+0

Aggiornato la mia risposta a una soluzione praticabile: è qui che si utilizzano quei codici enumerati –

+0

Ha un problema. Premere un altro tasto arbitrario dopo aver tenuto premuti i tre tasti e vedere che attiva la finestra di messaggio. –

3

Bene, questo è difficile dal momento che questi sono tutti i tasti di modifica e l'utente li potessero premere in qualsiasi ordine. Dovrai fare un po 'di filtraggio per assicurarti che una quarta chiave non generi ancora una corrispondenza, un problema con la risposta accettata. E il tasto di spostamento a destra è difficile, viene segnalato come Keys.Shift quando premuto. Ciò richiede Pinvoke per verificare se la chiave è giù.

Questo ha funzionato bene:

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown 
    If Control.ModifierKeys = (Keys.Control Or Keys.Alt Or Keys.Shift) Then 
     If e.KeyCode = Keys.ControlKey Or e.KeyCode = Keys.Menu Or e.KeyCode = Keys.ShiftKey Then 
      If GetKeyState(Keys.RShiftKey) < 0 And GetKeyState(Keys.LShiftKey) >= 0 Then 
       MessageBox.Show("yada") 
      End If 
     End If 
    End If 
End Sub 

Private Declare Function GetKeyState Lib "user32.dll" (ByVal key As Keys) As Short 

Questo funziona prima verificando che tutti e tre i tasti modificatori sono giù. Quindi controlla che l'ultimo tasto premuto sia stato uno dei delle tre chiavi, il filtro che garantisce che non si ottengano troppe corrispondenze. Infine controlla se il tasto di spostamento verso destra è abbassato e non è arrivato premendo anche il tasto sinistro.

+0

Grazie Hans, anche il tuo esempio funziona come previsto ma le risposte Yuri prima. –

+0

Non è la stessa risposta. –

+0

Ovviamente no, ma entrambi basati su user32.dll e ben noti GetKeyState. Pensi che la soluzione di Yuri non sia abbastanza buona? Con i test si comporta bene. –

1

Date un'occhiata a questo:

If e.Control And e.Alt And e.KeyCode = Keys.ShiftKey Then 
     MsgBox("CTRL + ALT + SHIFT") ' This work 
     Debug.Print("CTRL + ALT + SHIFT" & GetAsyncKeyState(Keys.ShiftKey) & GetAsyncKeyState(Keys.RShiftKey)) 
    End If 

    If e.Control And e.Alt And e.KeyCode = Keys.RShiftKey Then 
     MsgBox("CTRL + ALT + RIGHT SHIFT") ' This don't work 
     Debug.Print("CTRL + ALT + RIGHT SHIFT " & GetAsyncKeyState(Keys.ShiftKey) & GetAsyncKeyState(Keys.RShiftKey)) 
    End If 

vedrete che il valore per Keys.ShiftKey è lo stesso per destra e sinistra. Il test per Keys.RShiftKey cambia. Il DECLARE di cui sopra è richiesto per la chiamata API.

+0

Esperimento interessante che dà "praticabile questo": se e.Control e e.Alt e GetAsyncKeyState (Keys.RShiftKey) Allora ... –

Problemi correlati