KeyPress() - KeyRelease() - Key: differenze tra le versioni

Da Gambas-it.org - Wikipedia.
Vai alla navigazione Vai alla ricerca
(Creata pagina con "Con gli Eventi '''_KeyPress()''' e '''_KeyRelease()''' è possibile interagire con il programma Gambas mediante la tastiera del computer. "_KeyPress()" intercetta l'Evento de...")
 
Nessun oggetto della modifica
Riga 9: Riga 9:
  Public Sub Form_KeyPress()
  Public Sub Form_KeyPress()
   
   
   Dim a as string
   Dim s as string
   
   
  <FONT color=#006400>' ''Acquisisce il testo della chiave (il tasto premuto):''</font>
  <FONT color=#006400>' ''Acquisisce il testo della chiave (il tasto premuto):''</font>
   a = Key.Text
   s = Key.Text
   
   
   Print "E' stato premuto il tasto: "; a
   Print "E' stato premuto il tasto: "; s
   
   
  End
  End
Riga 21: Riga 21:
  Public Sub Form_keyRelease()
  Public Sub Form_keyRelease()
   
   
   Dim b as string
   Dim s as string
   
   
  <FONT color=#006400>' ''Acquisisce il testo della chiave (il tasto schiacciato):''</font>
  <FONT color=#006400>' ''Acquisisce il testo della chiave (il tasto schiacciato):''</font>
   b = Key.Text
   s = Key.Text
   
   
   Print "E' stato rilasciato il tasto: ";  b
   Print "E' stato rilasciato il tasto: ";  s
   
   
  End
  End
Riga 32: Riga 32:


===Altre modalità di utilizzo della funzione ''key''===
===Altre modalità di utilizzo della funzione ''key''===
<FONT color=#B22222><B>1 -</b></font>
  Public Sub Form_KeyPress()
  Public Sub Form_KeyPress()
   
   
Riga 48: Riga 47:
   
   
  End
  End
====Intercettando il tasto "''Control'' " + un altro tasto====
Qui intercettando il tasto "Control" + il tasto "R":
Public Sub Form_KeyPress()
<FONT color=#006400>' ''Se viene premuto il tasto "Control" + il tasto della lettera "R":''</font>
  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 \"!\""
Qui intercettando il tasto "Return" della tastiera madre o il tasto "Enter" della tastierina numerica:
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
   If (key.Code = Key["Return"]) Or (key.Code = Key["Enter"]) Then
Riga 66: Riga 55:
     Print "E' stato premuto il tasto \"Invio\"."
     Print "E' stato premuto il tasto \"Invio\"."
   Endif
   Endif
Qui intercettando il tasto "Shift" + il tasto "Canc" della tastierina numerica:
  If Key.Code = Key["Delete"] Then Print "E' stato premuto il tasto \"Canc\" della tstierina numerica."
o anche così:
o anche così:
  If Key.Code = Key.Delete Then Print "E' stato premuto il tasto \"Canc\" della tstierina numerica."
<FONT color=#B22222><B>2 -</b></font>
  Public Sub Form_KeyPress()
  Public Sub Form_KeyPress()
   
   
<FONT color=#006400>' ''Se viene premuto uno dei due tasti "Invio":''</font>
   Select Case Key.Code
   Select Case Key.Code
     Case Key.Enter, Key.Return
     Case Key.Enter, Key.Return
Riga 80: Riga 64:
   
   
  End
  End
<FONT color=#B22222><B>3 -</b></font>
Qui intercettando il numero del codice del tasto premuto:
  Public Sub Form_KeyPress()
  Public Sub Form_KeyPress()
   
   
Riga 87: Riga 71:
   
   
  End
  End
====Intercettando il tasto "''Control'' " + un altro tasto====
Qui intercettando il tasto "Control" + il tasto "R":
Public Sub Form_KeyPress()
<FONT color=#006400>' ''Se viene premuto il tasto "Control" + il tasto della lettera "R":''</font>
  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" della tastierina numerica:
  If Key.Code = Key["Delete"] Then Print "E' stato premuto il tasto \"Canc\" della tstierina numerica."
o anche così:
  If Key.Code = Key.Delete Then Print "E' stato premuto il tasto \"Canc\" della tstierina numerica."


===Utilizzare la Classe ''Window''===
===Utilizzare la Classe ''Window''===

Versione delle 10:47, 31 ott 2023

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\"."

o anche così:

 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" della tastierina numerica:

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

o anche così:

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

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