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

Da Gambas-it.org - Wikipedia.
Riga 11: Riga 11:
 
   Dim s as string
 
   Dim s as string
 
   
 
   
  <FONT color=#006400>' ''Acquisisce il testo della chiave (il tasto premuto):''</font>
+
  <FONT color=gray>' ''Acquisisce il testo della chiave (il tasto premuto):''</font>
 
   s = Key.Text
 
   s = Key.Text
 
   
 
   
Riga 23: Riga 23:
 
   Dim s as string
 
   Dim s as string
 
   
 
   
  <FONT color=#006400>' ''Acquisisce il testo della chiave (il tasto schiacciato):''</font>
+
  <FONT color=gray>' ''Acquisisce il testo della chiave (il tasto schiacciato):''</font>
 
   s = Key.Text
 
   s = Key.Text
 
   
 
   
Riga 34: Riga 34:
 
  Public Sub Form_KeyPress()
 
  Public Sub Form_KeyPress()
 
   
 
   
  <FONT color=#006400>' ''Se viene premuto il tasto del carattere “R”:''</font>
+
  <FONT color=gray>' ''Se viene premuto il tasto del carattere “R”:''</font>
 
   If Key.Code = Key["R"] Then Print "E' stato premuto il tasto della lettera \"R\"."
 
   If Key.Code = Key["R"] Then Print "E' stato premuto il tasto della lettera \"R\"."
 
   
 
   
Riga 41: Riga 41:
 
  Public Sub Form_KeyPress()
 
  Public Sub Form_KeyPress()
 
   
 
   
  <FONT color=#006400>' ''Se viene premuto il tastino della freccia a destra:''</font>
+
  <FONT color=gray>' ''Se viene premuto il tastino della freccia a destra:''</font>
 
   If Key.Code = Key["Right"] Then Print "E' stato premuto il tasto \"Right\"."
 
   If Key.Code = Key["Right"] Then Print "E' stato premuto il tasto \"Right\"."
 
   
 
   
Riga 71: Riga 71:
 
  Public Sub Form_KeyPress()
 
  Public Sub Form_KeyPress()
 
   
 
   
  <FONT color=#006400>' ''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:''</font>
+
  <FONT color=gray>' ''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:''</font>
 
   If Key.Code = 65 then Print "E' stato premuto il tasto con il carattere \"A\"."
 
   If Key.Code = 65 then Print "E' stato premuto il tasto con il carattere \"A\"."
 
   
 
   
Riga 80: Riga 80:
 
  Public Sub Form_KeyPress()
 
  Public Sub Form_KeyPress()
 
   
 
   
  <FONT color=#006400>' ''Se viene premuto il tasto "Control" + il tasto della lettera "R":''</font>
+
  <FONT color=gray>' ''Se viene premuto il tasto "Control" ("Ctrl") + il tasto della lettera "R":''</font>
 
   If Key.Code = Key["R"] And Key.Control Then Print "Hai premuto CTRL+R"
 
   If Key.Code = Key["R"] And Key.Control Then Print "Hai premuto CTRL+R"
 
   
 
   
Riga 86: Riga 86:
 
Qui intercettando il tasto "Control" + il tasto "!":
 
Qui intercettando il tasto "Control" + il tasto "!":
 
   If Key.Code = Key["!"] And Key.Control Then Print "E' stato premuto 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====
 +
Per intercettare la pressione combinata del tasto "Shift" con un altro tasto, bisognerà impostare la Costante ".Shift".
 +
<BR>In questo esempio si intercetta la pressione del tasto "Shift" + il tasto "R":
 +
Public Sub Form_KeyPress()
 +
 +
<FONT color=gray>' ''Se viene premuto il tasto "Shift" + il tasto della lettera "R":''</font>
 +
  If Key.Code = Key["R"] And Key<FONT color=red>.Shift</font> Then Print "Hai premuto Shift+R"
 +
 +
End
 +
 +
  
  

Versione delle 03:57, 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 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" ("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

Per intercettare la pressione combinata del tasto "Shift" con un altro tasto, bisognerà impostare la Costante ".Shift".
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



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 la lettera "R" maiuscola (tasto "Schift" + tasto "R"), lo rileva in console:
 If Key.Code = key["R"] Then Print "Evento sollevato: tasto \"R\" premuto !"
   
End