Differenze tra le versioni di "KeyPress() - KeyRelease() - Key"

Da Gambas-it.org - Wikipedia.
 
(Nessuna differenza)

Versione attuale delle 04:47, 16 lug 2024

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 tasto "TAB":

Public Sub Form_KeyPress()

 If Key.Code = Key.Tab Then Print "E' stato premuto il tasto \"TAB\" !"

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 la pressione di due o tre tasti combinata in sequenza

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" ("Ctrl") + 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

Si possono avere due casi:
1) per intercettare la pressione del tasto "Shift" + un altro tasto è prevista una unica specifica Costante della Classe Key.
Il caso è ad esempio quello del tasto "BackTab", che si ottiene premendo in sequenza il tasto "Shift" + il tasto "TAB".
Ebbene, per intercettare questo effetto in Gambas, basterà impostare la specifica Costante prevista appunto dalla Classe Key: "Key.BackTab".
Esempio pratico:

If Key.Code = Key.BackTab Then Print "E' stato premuto il tasto \"BACKTAB\" ("hift" + "Tab" !"


2) per intercettare la pressione del tasto "Shift" + un altro tasto, bisognerà prendere in considerazione tali due tasti premuti della tastiera.
In tal caso, per intercettare la pressione combinata del tasto "Shift" con un altro tasto, bisognerà impostare la Costante ".Shift".
In particolare nella riga del condizionale va posto prima il riferimento al tasto generico prescelto e dopo l'operatore AND va posto il riferimento al tasto "Shift":

If Key.Code = Key["R"] And Key.Shift Then ......

In questo esempio si intercetta la pressione del tasto "Shift" + il tasto "R":

Public Sub Form_KeyPress()

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

End

Intercettando tre tasti: "Control" + "Shift" + un altro tasto

In ordine alla pressione di tre tasti nella riga del condizionale va posto prima il riferimento al tasto generico prescelto, dopo vanno posti i riferimenti ai tasti "Control" e "Shift" con i rispettivi operatori AND:

Public Sub Form_KeyPress()

' Se viene premuto il tasto "Control" + il tasto "Shift" + il tasto della lettera "F":
 If Key.Code = Key["F"] And Key.Control And Key.Shift Then Print "Hai premuto in modo combinato i tasti: Control+Shift+F !"

End


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"
' Si dà il "focus" alla finestra virtuale:
   .SetFocus
 End With
 
End

 
Public Sub Evento_keyPress()

' Se si preme il tasto "R" della tastiera, lo rileva in console:
 If Key.Code = key["R"] Then Print "Evento sollevato: tasto \"R\" premuto !"
   
End