The onchar global function allows you to trap characters in order to perform special actions or to change the action of the character. It can also be used to disable or remap characters. This function must be placed in the Global Procedure.
If an onchar global function is coded, every character the operator types is sent to the onchar function for processing. If the onchar function returns a value, then the return value is processed by the field as the character. If a statement in the onchar function causes movement to another field within the case, then the movement is executed. If no onchar function is coded, then characters are unmodified.
The key value is a number code identifying what character was typed using the keyboard. Its value can be used within the function.
You can use the OnKey Character Map to determine the value of characters.
Calls to this function are ignored when executed on mobile devices.
Differences between onkey and onchar:
The onchar function differs from the onkey function. A keystroke contains information about the key pressed on the keyboard, regardless of what this keystroke eventually maps to. A character refers to the final representation of one or more keystrokes. This is important when using non-Latin languages that require multiple keystrokes to create one character. For example, creating the Chinese character 'é©¬' using a Pinyin input system requires two keystrokes: 'm' and 'a.' The code for this character is 39532. If typing such a keystroke, onkey will be called several times before onchar is called. If both onkey and onchar functions exist, onkey will always be called before onchar is called.
The onchar function also returns values different from the onkey function for some Latin keystrokes. For example, with Caps Lock off, if a keyer holds down Shift and types 'M,' onkey will return 1077 (1000 for the shift, 77 for 'm'). Onchar, on the other hand, will return 77, the character code for 'M.' For a lowercase 'm,' onkey returns 77 and onchar returns 109, the character code for 'm.'
The onchar function does not return any information about whether any of the Shift, Ctrl, or Alt keys were held down when the character was typed.
Like any global function, the onchar global function returns an integer value. The value should be either the value of the character pressed (the same as the value passed to the function), a substituted character value (remapping the character), or zero (0) to indicate that the character is to be ignored.