Categorised Menu List

KeyPress Event Handller

How KeyPress Event is more useful in programming

KeyDown Event - When a key is Pressed and not yet Released, the code in this event is executed as soon as you press the any key or to the key to which the program is written.

KeyUp Event - When a key is released, but not when any key is pressed
=> meaning... when a key is pressed, no code works or executed, unless there is another piece of code writen for

 

KeyDown Event.
Key Press - When a key on the keyboard is pressed and relesed... ie., when a key is press and released immediately, and not after some time.
To summarise: Three keyboard events namely: KeyDown, KeyUp, KeyPress.

Please refer to chr() and asc() in excel or in access. This will help, to understand, what an ASCII (American Standard Code for Information and Interchange) code is and what does chr() and asc() are...

gallery/keypresseventcode

Event Handler:

Event handler is nothing more than a procedure/function. Events are executed

  • either at user interaction with system or when a user presses a key on the keyboard (ie., on Mouse-Left-Button-click or Mouse-double-click-left-button.
gallery/keypress

Points to Note: 

  • The above code checks whether or not to print certain characters.
  • This code is very simple and no mastery ever required.
  • Description: txtUserControl is a text-box control on the user form, into which, certain characters should be restricted from accepting.
  • Procedure: Place a text box on Excel UserForm or on excel worksheet or access form. Double click on the control or press Alt + F11 to move to code window.
  • If, moved to code window, through Alt + F11, then, Select the control name from the left drop down list box in the code window and select the KeyPress Event from the right drop down window.
  • Copy and paste the above code and Press F5 to run the code.
  • SELECT CASE is a programming construct used instead of IF Construct

Examples to Practice:

  • Print only vowels in a given text box
  • Print only consonents in a given text box
  • Write code to convert each numeric character that is typed, to '*', just similar to password, ie., A text box should accept only numeric characters, also each numeric character that is keyed-in should be converted into '*'
  • Write code to accept only first five alphabets and first five numbers and not any other character.
  • Write code to accept only upper case letters and not lower case letters.
  • Write code to accept only lower case letters, if the user types upper case letters, then the programme should automatically convert upper case letters into lower case letters and print in text box.
  • Write code to print only special characters and none (eg: @@, #, $, %, ^, ...")
  • Write code to covert any character to into special character.

All the content that you learn here will not be of any use, until you practice, so I urge you to practice. As and when you practice, you get more doubts as to how to go ahead, these doubts strengthen your knowledge and lead you to experience. 

gallery/always end in sight
  • or with system interaction with system (ie., idle event - executed when the system is doing no activity from user's persective, a good example is screen saver, or auto-screen lock or a scheduler which runs automatically when said condition is met etc...)