mScriptBox Tutorials
Introduction to Keyup And Keydown Events

Written by Feared
Published with permission.

Table Of Contents

  1. Introduction
  2. The Syntax
  3. The $keyval Identifier
  4. The $keyrpt Identifier
  5. Conclusion

1. Introduction  Back to Top

These two events are in the help file but aren't explained very well. This is why I have taken the time to write this for you. Although they aren't very well known, they do have very interesting properties.

The on KEYDOWN and on KEYUP events are triggered when you type a specific key on your keyboard in a @window. These are very useful for a GUI window. You can even code a game or something.

2. The Syntax  Back to Top

The format is as follows:
on LEVEL:KEYDOWN:@window:N: { commands }
on LEVEL:KEYUP:@window:N: { commands }
The @window is the specified window you type in.
Note: This cannot be a #channel window or "Status Window" - Only windows in which do not contain a listbox or an editbox.

The N is the number of the key you typed. These numbers are sometimes ASCII numbers, but many are different. You can also put in wildcards like:

on 1:KEYDOWN:@window:*: { commands }
This event will trigger on any key you type in the specified @window.
Note: It's the exact same way with the on 1:KEYUP: event. Only, the on 1:KEYUP: event triggers when you lift a key after you have pressed it.

3. The $keyval Identifier  Back to Top

These events include two different identifiers: $keyval, and $keyrpt (Explained below in Section 4). The $keyval identifier returns the numeric code for the key you just pressed. If you pressed the space bar, it would return the code, 32. Remember that not all codes are the same as ASCII codes assigned to the character. For example:
on 1:KEYDOWN:@window:*: { 
  ; This is the same as the ASCII code. 
  IF ($keyval == 32) { echo -s You have pressed spacebar in @window } 
  ; This is NOT the ASCII code for the period key. 
  IF ($keyval == 190) { echo -s You have pressed the period key } 
The ASCII code for a period is 46, but the $keyval identifier returns 46 when the delete key is pressed. A good way to find out which codes belong to which keys, is this:
on 1:KEYDOWN:@window:*: { 
  echo -s $keyval 
This will echo in your status window the number assigned to the key you just pressed. Every single key on your keyboard will return a unique number.

Also note that all letters typed return ASCII codes assigned to capitalized letters.

4. The $keyrpt Identifier  Back to Top

Unlike the $keyval identifier, this identifier is a lot simpler. It returns $true if you hold on to a key while pressing it in the @window. It returns $false if you just pressed it once.
For example:
on 1:KEYDOWN:@window:32: { 
  IF ($keyrpt == $true) { HALT } 
  IF ($keyrpt == $false) { echo -s You pressed the space bar } 
This will halt the script when you hold on to the space bar, and will echo if you have pressed it only once. Simple, eh?

5. Conclusion  Back to Top

I wouldn't recommend trying to use these events unless you know windows well. This is probably for advanced scripters only. I hope I have explained this well and may you have gained more insight as to how the keyboard, and mIRC works. :)