Occur when the user presses (KeyDown) or releases (KeyUp) a key while an object has the focus.
Sub object_KeyDown([index%,] Code&, Shift&)
Sub object_KeyUp([index%,] Code&, Shift&)
object | :Ocx Object |
index | :iexp |
Code&, Shift& | :Short exp |
The KeyDown and KeyUp event syntaxes have these parts:
index% - An integer that uniquely identifies a control or form if it's in an array.
Code& - A key code, such as VK_F1 (the F1 key) or VK_HOME (the HOME key). To specify key codes, use the API VK_* constants. For a comprehensive list of key codes, see Key Codes and ASCII Values.
shift& - An integer that corresponds to the state of the SHIFT, CTRL, and ALT keys at the time of the event. The shift argument is a bit field with the least-significant bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2).
Shift | 1 |
Control | 2 |
Alt | 4 |
These bits correspond to the values 1, 2, and 4, respectively. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys are pressed. For example, if both CTRL and ALT are pressed, the value of shift is 6.
KeyDown and KeyUp aren't invoked for:
- The ENTER key if the form has a Command control with the Default property set to True.
- The ESC key if the form has a Command control with the Cancel property set to True.
- The TAB key.
KeyDown and KeyUp interpret the uppercase and lowercase of each character by means of two arguments: keycode, which indicates the physical key (thus returning A and a as the same key) and shift, which indicates the state of shift + key and therefore returns either A or a.
Form frm1 = "KeyDown, KeyUp Events", 20, 20, 300, 300
Do
Sleep
Until Me Is Nothing
Sub frm1_KeyDown(Code&, Shift&)
Print "KeyDown - Code& = "; Code&
If Shift& And 1
Print "Shift pressed"
Else If Shift& And 2
Print "Ctrl pressed"
Else If Shift& And 4
Print "Alt pressed"
EndIf
EndSub
Sub frm1_KeyUp(Code&, Shift&)
Print "KeyUp - Code& = "; Code&
If Shift& And 1
Print "Shift pressed"
Else If Shift& And 2
Print "Ctrl pressed"
Else If Shift& And 4
Print "Alt pressed"
EndIf
EndSub
Use the Screen_KeyPreview event to create global keyboard-handling routines. This event sub receives these events before controls or the form receives the events.
According to previous documentation, by setting Code& to 0, the keypress would then be ignored; this does not happen.
To cancel the input from the keyboard, there are two methods available:
OpenW 1 : Win_1.PrintWrap = True : FontName = "courier new"
Do : Sleep : Until Win_1 Is Nothing
Sub Win_1_KeyPress(Ascii&)
If Chr(Ascii&) = "A" Or Chr(Ascii&) = "a"
Ascii& = 0
Else
Print Chr(Ascii&);
EndIf
EndSub
Global diskey?, disv?
OpenW 1
Ocx TextBox tb = "", 10, 10, 200, 150 : .MultiLine = True : .BorderStyle = 1
Ocx Command cmd = "Disable Ctrl-V", 230, 10, 100, 22
Clipboard.SetText "GFA Basic "
tb.SetFocus
Do : Sleep : Until Win_1 Is Nothing
Sub cmd_Click
Local tbss As Int32 = tb.SelStart // Stores Textbox Caret position
If disv? Then cmd.Caption = "Disable Ctrl-V"
If Not disv? Then cmd.Caption = "Enable Ctrl-V"
disv? = Not disv?
tb.SetFocus // Returns focus to Textbox
tb.SelStart = tbss // Replaces the caret where it was
EndSub
Sub tb_KeyDown(Code&, Shift&)
Debug Code&
If disv? // If Ctrl-V is disabled
If Shift& = 2 And Code& = 86 // If Ctrl-V pressed
diskey? = True // Disable Keypress
EndIf
EndIf
EndSub
Sub tb_KeyPress(Ascii&)
If diskey?
Ascii& = 0 // Cancels the keypress
diskey? = False // Resets the diskey? flag
EndIf
EndSub
On occasions when using the example above with different 'shift key' combinations, the key press after the disqualified key combination may be ignored or not printed; if this happens, insert 'diskey? = False' as the first line of the KeyDown sub.
Form, KeyPress, Screen_KeyPreview
{Created by Sjouke Hamstra; Last updated: 01/03/2017 by James Gaite}