• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
      • Statement Format Symbols
      • Alphabetical List of Functions and Statements
      • List of Reserved Words
      • Deprecated Features
      • Declaration Statements
      • Symbol Functions
      • Item Functions
      • Array Object
      • Audio Object
      • Barcode and QR Codes
      • Case Object
      • Document Object
      • File Object
      • Freq Object
      • Geometry Object
      • HashMap Object
      • Image Object
      • List Object
      • Map Object
      • Path
      • Pff Object
      • SystemApp Object
      • ValueSet Object
      • Program Control Statements
      • Assignment Statements
      • Data Entry Statements and Functions
        • Accept Function
        • Advance Statement
        • Ask Statement
        • ChangeKeyboard Function
        • Connection Function
        • DeMode Function
        • Display Orientation
        • EditNote Function
        • EndLevel Statement
        • EndGroup Statement
        • Enter Statement
        • GetCaptureType Function
        • GetCaseLabel Function
        • GetDeviceID Function
        • GetImage Function
        • GetNote Function
        • GetOperatorId Function
        • GetOS Function
        • GetRecord Function
        • GetUserName Function
        • GPS Function
        • Interactive GPS Modes
        • HideOcc Function
        • Highlighted Function
        • InAdvance Function
        • IsPartial Function
        • IsVerified Function
        • Move Statement
        • NoInput Statement
        • OnChangeLanguage Global Function
        • OnChar Global Function
        • OnKey Global Function
        • OnStop Global Function
        • OnSystemMessage Global Function
        • Prompt Function
        • Protect Function
        • PutNote Function
        • RandomizeVS Function
        • Reenter Statement
        • SavePartial Function
        • SelCase Function
        • Set Attributes Statement
        • Set Behavior CanEnter Statement
        • SetCapturePos Function
        • SetCaptureType Function
        • SetCaseLabel Function
        • Set ErrMsg Statement
        • SetFont Function
        • SetOperatorId Function
        • Show Function
        • ShowArray Function
        • ShowOcc Function
        • Skip Statement
        • Userbar Function
        • VisualValue Function
      • Batch Edit Statements
      • Numeric Functions
      • String Functions
      • Multiple Occurrence Functions
      • General Functions
      • Date and Time Functions
      • External File Functions
      • Synchronization Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

OnKey Global Function

Format
function numeric OnKey(numeric key_value);
Description
The OnKey global function allows you to trap keystrokes in order to perform special actions or to change the action of the key. It also can be used to disable or remap keys. This function must be placed in the Global procedure.
If an OnKey global function is coded, every keystroke the operator types is sent to the OnKey function for processing. If the OnKey function returns a value, then the return value is processed by the field as the keystroke. If a statement in the OnKey function causes movement to another field within the case, then the movement is executed. If no OnKey function is coded, then keystrokes are unmodified.
The key value is a number code identifying what key was pressed on the keyboard. Its value can be used within the function. See detailed description below.
You can use the OnKey Character Map to determine the value of characters.
Calls to this function are ignored when executed on mobile devices.
Return Value
The OnKey function must return a number. The value should be either the value of the key pressed (the same as the value passed to the function), a substituted key value (remapping the key), or zero (0) to indicate that the key is to be ignored.
Example
function numeric OnKey(numeric key_value)

   
// F3, F4, F5: don't allow these keys to work, eat the key
    if key_value in 114:116 then
       
OnKey = 0;

   
// Ctrl+E goes to END_FIELD
    elseif key_value = 2069 then
       
move to END_FIELD;

   
// return the value of any other key
    else
       
OnKey = key_value;

   
endif;

end;
Keyboard Key Values
A summary of key values passed into and out of the OnKey function are given below. If in doubt, use the OnKey Character Map to determine the value of characters. It can be accessed from CSPro's View menu option.
Numbers
For the number keys across the top of the keyboard or on the numeric keypad when the "NumLock" button is depressed, the value 48 should be added to the number. For example, the keyboard key value returned for the range 0-9 is 48-57 respectively.
Letters
For the 26 a-z letters the code range 65-90 will be returned. If uppercase letters are sought, then you must add the Shift key value (1000) to the 65-90 range, yielding 1065-1090. If the Ctrl and/or Alt keys are being held down that will impact the value returned too; however, the CapsLock key does not impact the code returned.
Control Keys
If the control keys Shift, Ctrl, or Alt are pressed in isolation, i.e., are not combined with any other keystroke, they will have the following key values:
KeyCode
Shift1016
Ctrl2017
Alt4018
Control Key Combos
If the control keys Shift, Ctrl, and/or Alt are pressed in combination with another key, the following base values should be added to the code of the other key.
KeysAdd
Shift1000
Ctrl2000
Shift+Ctrl3000
Alt4000
Alt+Shift5000
Alt+Ctrl6000
Alt+Shift+Ctrl7000
For example, if Shift, Ctrl, and/or Alt are held down when 'a' is pressed, it causes the following codes:
KeysCode
a65
Shift+a1065
Ctrl+a2065
Shift+Ctrl+a3065
Alt+a4065
Alt+Shift+a5065
Alt+Ctrl+a6065
Alt+Shift+Ctrl+a7065
Function Keys
The 12 function keys F1-F12 return the key value 112-123.
Non-numeric Numpad Keys
KeyCode
NumLock144
*106 (with or without NumLock)
+107 (with or without NumLock)
-109 (with or without NumLock)
.110 (with or without NumLock)
/111 (with or without NumLock)
Miscellaneous Keys
KeyCode
SysReqno code returned
Bksp8
Tab9
Enter13 (with or without NumLock)
Break19
Caps20
Escape27
Space32
Page Up33
Page Down34
End35
Home36
Left Arrow37
Up Arrow38
Right Arrow39
Down Arrow40
Insert45
Delete46
Wnd91 (the flying window)
Scroll Lock145
;186
=187
,188 (comma)
-189 (dash/minus)
.190 (dot/period)
/191
`192 (accent)
[219
]221
\220
'222 (regular quote)
See also: CSPro Program Structure, User-Defined Functions, OnKey Character Map