• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Key Object

The Key object consists of just a few methods, several constants, and a couple of listeners. The methods primarily just enable you to ascertain whether a particular key is currently being pressed (whether it “is down”) or, in the case of Caps Lock or Num Lock, whether a key “is toggled.” The constants (simply properties that never change) are all associated with particular keys. That is, to check whether a key is pressed, you need to specify which key. Each key is a constant (such as Shift, Left Arrow, Delete, and so on).

Using the Key Object

To use the Key object, use the form Key.isDown(whichKey), which returns either true or false and in which whichKey is either the Key object constant for that key or the “virtual key code” for that key. Virtual key codes are almost identical to ASCII. However, whereas ASCII applies only to numbers and characters that appear in strings, virtual key codes are extended to include other keys, such as Shift and Ctrl. What’s really funky is that the virtual key code for an alphanumeric key happens to be the same as that key’s uppercase ASCII. Although ASCII distinguishes between uppercase and lowercase, it includes only letters and numbers. Virtual key codes, on the other hand, include all keys but don’t recognize any difference between uppercase or lowercase. The ASCII for “A” is 65 (as is the virtual key code for the “A” key). However, the ASCII for “a” is 97, whereas 97 happens to be the virtual key code for the “1” on a keyboard’s number pad. You’ll see that it’s easy to determine any key’s ASCII or virtual key code. For a practical example of the isDown() method, consider that the virtual key code for the Tab key happens to be 9. Both of the two following expressions will return true if the user happens to press the Tab key:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint