Capture Keyboard Input '------------------------------------------------------ 'How To: Capture Keyboard Input 'Posted: June 1998 'From: "Robert L. Walsh" rlwalsh@capecod.net '------------------------------------------------------ Keywords to see in on-line help: KeyPreview property, KeyDown, KeyUp and KeyPress events, ActiveControl property.
If you need to capture a user`s keyboard input _before_ the keystroke reaches any of the controls mounted on a form, set your form's KeyPreview property to True, and process each keystroke in the form`s KeyDown, KeyUp or KeyPress events. Use the KeyPress event for regular alphanumeric (ANSI) keystrokes and the KeyDown or KeyUp events for all other keys (Escape, Backspace, Enter, Arrow keys, Function Keys, etc.). A simple example:
Private Sub Form_KeyPress(KeyAscii As Integer) If ActiveControl.Name = "txtName" Then 'If txtName has the focus... If KeyAscii < 65 Or KeyAscii > 90 Then 'If keystroke is NOT a capital letter. If KeyAscii >=97 And KeyAscii <=122 Then'If lower case... KeyAcsii = (KeyAscii Or 32) 'Convert to upper case. Else KeyAscii = 0 'Set keystroke to zero. End If End If
The above is a brief example of the control your code can have over keyboard input. The code fragment simply filters out any characters that are not upper case. If the character received is lower case, it is converted to caps, otherwise the character is zeroed out. Setting the keystroke value to zero will be interpreted by most controls as a null character and will therefore be ignored. You can also set the keystroke to some other default value depending on the control receiving the keystroke.
Downloads In IE right-click and select 'Save Target As...' or in Netscape
right-click and select 'Save Link As...' View Plain Text
Not Available
Not Available
|