KeyPress() - KeyRelease() - Key

Da Gambas-it.org - Wikipedia.

Con gli Eventi _KeyPress() e _KeyRelease() è possibile interagire con il programma Gambas mediante la tastiera del computer.

"_KeyPress()" intercetta l'Evento dello schiacciamento di uno o più tasti della tastiera, e sin tanto che essi vengono mantenuti premuti.

"_KeyRelease()" intercetta invece il rilascio del tasto.


Per intercettare genericamente ogni Evento legato alla pressione di un tasto della tastiera, si legheranno gli Eventi "_KeyPress()" e "_KeyRelease()" al Form, il quale dovrà avere in quel momento il Focus.

Public Sub Form_KeyPress()

 Dim s as string

' Acquisisce il testo della chiave (il tasto premuto):
 s = Key.Text

 Print "E' stato premuto il tasto: "; s

End


Public Sub Form_keyRelease()

 Dim s as string

' Acquisisce il testo della chiave (il tasto schiacciato):
 s = Key.Text

 Print "E' stato rilasciato il tasto: ";  s

End

E' possibile ovviamente utilizzare gli Eventi "_Keypress()" e "_Keyrelease()" di un Oggetto grafico qualsiasi che li supporti (ad esempio un Button). Anche in questo caso, affinché detti Eventi possano essere sollevati, è necessario che l'Oggetto, ad esso legati, abbia in quel momento il Focus.

Altre modalità di utilizzo della funzione key

Public Sub Form_KeyPress()

' Se viene premuto il tasto del carattere “R”:
 If Key.Code = Key["R"] Then Print "E' stato premuto il tasto della lettera \"R\"."

End

Qui intercettando il tasto freccia a "destra":

Public Sub Form_KeyPress()

' Se viene premuto il tastino della freccia a destra:
 If Key.Code = Key["Right"] Then Print "E' stato premuto il tasto \"Right\"."

End

o anche così:

Public Sub Form_KeyPress()

 If Key.Code = Key.Right Then Print "E' stato premuto il tasto \"Right\"."

End

Qui intercettando il tasto "Return" della tastiera madre o il tasto "Enter" della tastierina numerica:

 If (key.Code = Key["Return"]) Or (key.Code = Key["Enter"]) Then
   Print "E' stato premuto il tasto \"Invio\"."
 Endif

o anche così:

 If (key.Code = Key.Return) Or (key.Code = Key.Enter) Then
   Print "E' stato premuto il tasto \"Invio\"."
 Endif

o anche così:

Public Sub Form_KeyPress()

 Select Case Key.Code
   Case Key.Enter, Key.Return
     Print "E' stato premuto il tasto \"Invio\"."
 End Select

End

Qui intercettando il numero del codice del tasto premuto:

Public Sub Form_KeyPress()

' Si prende in considerazione il numero del codice del tasto premuto, che - in questo caso - corrisponde al numero di codice ASCII del carattere collegato al tasto premuto:
 If Key.Code = 65 then Print "E' stato premuto il tasto con il carattere \"A\"."

End

Intercettando il tasto "Control " + un altro tasto

Qui intercettando il tasto "Control" + il tasto "R":

Public Sub Form_KeyPress()

' Se viene premuto il tasto "Control" + il tasto della lettera "R":
 If Key.Code = Key["R"] And Key.Control Then Print "Hai premuto CTRL+R"

End

Qui intercettando il tasto "Control" + il tasto "!":

 If Key.Code = Key["!"] And Key.Control Then Print "E' stato premuto il tasto \"Control\" + il tasto \"!\""

Intercettando il tasto Shift + un altro tasto

Qui intercettando il tasto "Shift" + il tasto "Canc":

 If Key.Code = Key["Delete"] Then Print "E' stato premuto il tasto \"Canc\"."

o anche così:

 If Key.Code = Key.Delete Then Print "E' stato premuto il tasto \"Canc\"."

Utilizzare la Classe Window

Se non si intende utilizzare il Form, né un controllo specifico insistente sul Form, è possibile servirsi della Classe Window creando una finestra virtuale, che sarà gestita mediante una variabile:

Private w As Window


Public Sub Form_Open()
 
 With w = New Window(FMain) As "Evento"
' Diamo il "focus" alla finestra virtuale:
   .SetFocus
 End With
 
End

 
Public Sub Evento_keyPress()
 
 If Key.Code = key["R"] Then Print "Evento sollevato: tasto \"R\" premuto !"
   
End