Forms

3.87 Intercettare la pressione di tasti e/o pulsanti del mouse
  Silvio Savoldi

Qui di seguito sono indicati alcuni metodi per intercettare le combinazioni di tasti e pulsanti del mouse per condizionare eventi od azioni.
Si ricorda che in ogni caso va impostata a Si la proprietà Anteprima tasti della maschera.
Quello che segue è il codice VBA che va generato a fronte dell'evento Su tasto giù della maschera per rilevare la pressione contemporanea di due o più tasti.
Private Sub XXXXX_KeyDown(KeyCode As Integer, Shift As Integer)
  Dim TastoShift As Integer, TastoAlt As Integer, TastoCtrl As Integer
  TastoShift = (Shift And acShiftMask) > 0
  TastoAlt = (Shift And acAltMask) > 0
  TastoCtrl = (Shift And acCtrlMask) > 0
  If TastoShift And TastoAlt And TastoCtrl Then
        MsgBox "SHIFT + ALT +CTRL"
  End If
 
  If TastoShift And TastoCtrl And KeyCode = vbKeyF10 Then
        MsgBox "SHIFT + CTRL + F10"
  End If
  
  If TastoCtrl And KeyCode = vbKeyY Then
        MsgBox "CTRL + Y"
  End If
End Sub
Quello che segue è invece il codice VBA da generare a fronte dell'evento Su pulsante mouse giù della maschera per rilevare la pressione di uno dei pulsanti del mouse.
Private Sub XXXXX_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
‘Rileva pressione pulsante Mouse destro, sinistro e centrale
  If Button = acLeftButton Then
        MsgBox "Tasto sinistro"
  ElseIf Button = acMiddleButton Then
        MsgBox "Tasto centrale"
  ElseIf Button = acRightButton Then
        MsgBox "Tasto destro"
  End If
End Sub
Infine quello che segue è il codice VBA da generare a fronte dell'evento Su pulsante mouse giù della maschera per rilevare la pressione contemporanea di un pulsante del mouse e del tasto ALT o SHIFT o CTRL.
Private Sub XXXXX_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
‘Rileva combinazioni Mouse - Tastiera
  Dim TastoShift As Integer, TastoAlt As Integer, TastoCtrl As Integer
  TastoShift = (Shift And acShiftMask) > 0
  TastoAlt = (Shift And acAltMask) > 0
  TastoCtrl = (Shift And acCtrlMask) > 0

  If TastoShift And Button = acLeftButton Then
        MsgBox "SHIFT + tasto sinistro Mouse"
  End If
  
  If TastoAlt And Button = acLeftButton Then
        MsgBox "ALT + tasto sinistro Mouse"
  End If
  
  If TastoCtrl And Button = acLeftButton Then
        MsgBox "CTRL + tasto sinistro Mouse"
  End If
  
  If TastoShift And Button = acRightButton Then
        MsgBox "SHIFT + tasto destro Mouse"
  End If
  
  If TastoAlt And Button = acRightButton Then
        MsgBox "ALT + tasto destro Mouse"
  End If
  
  If TastoCtrl And Button = acRightButton Then
        MsgBox "CTRL + tasto destro Mouse"
  End If
    
  If TastoShift And Button = acMiddleButton Then
        MsgBox "SHIFT + tasto centrale Mouse"
  End If
  
  If TastoAlt And Button = acMiddleButton Then
        MsgBox "ALT + tasto centrale Mouse"
  End If
  
  If TastoCtrl And Button = acMiddleButton Then
        MsgBox "CTRL + tasto centrale Mouse"
  End If
  End Sub


Se pensate di avere del materiale freeware interessante e volete pubblicarlo, allora leggete qui.