13 Keyboard Service
19Q Part III ■ MMURTL-Specific Details The keyboard is handled with a system service. As described in Chapter 1 0, “Systems Program- ming,” system services are accessed with the Request primitive. The keyboard is handled with a system service because it, like the file system, is a shared resource. Several programs can have outstanding requests to the keyboard service at one time. Only one program (job) will be as- signed to receive keystrokes at any one time. The exception to this rule is the global keyboard request to receive Ctrl-Alt keystrokes. The service name is KEYBOARD (uppercase as always). Each of the functions is identified by its service code number, here is an example of a keyboard service request: Request ( “KEYBOARD”, /* Service Name */ 1, /* wSvcCode for ReadKbd */ MyExch, /* dRespExch */ &MyRqhandle, /* pRqHndlRet */ 0, /* npSend (no Send Ptrs) */ &KeyCode /* pDatal */ 4, /* cbDatal (size of KeyCode)*/ 0 /* pData2 - not used (0) */ 0, /* Not used (0) */ 1 , /* dData0 - fWait for Key */ 0> /* dDatal - Not used (0) */ 0); /* dData2 - Not used (0) */ All message-based services use the same request interface. Using the request interface allows for asynchronous program operation. You can make a request and then go do something else be- fore you return to wait or to check the function to see if you received a response from your request. This is true multitasking. If you don't need asynchronous keyboard access, the public call ReadKbd ( ) is provided. This is a blocking call with only two parameters. It's easier to use but not as powerful. I'll describe it after I discuss the following services. Available Service Table 13.1 shows the services provided by the service code. Table 13.1. Available keyboard services. Service Code Function 1 Read Keyboard 2 Notify On Global Keys 3 Cancel Notify On Global Keys 4 Assign Keyboard The following sections describe these four functions.
Chapter 13 U Keyboard Service Read Keyboard The Read Keyboard function ( 1 ) allows a program to request keyboard input from the service. The first request pointer points to an unsigned dword where the key code will be returned. The key codes are described in detail in tables later in this chapter. The dDataB determines whether the service will hold the request until a key is available. A value of 0 means the request will be sent back to you immediately, even if a key is not available. The error from the service is ErcNoKeyAvailable (700) if no key was available. The key code is undefined if no key was available (so you must check the error). A value of 1 in dData0 causes the service to hold your request until a key is available. If a key was available, the error should be 0. The following are the Request parameters for Read Keyboard: wSvcCode = 1 npSend = 0 pDatai = Pointer where the KeyCode will be returned dcbDatai = 4 - Count of bytes in the code pData2 = 0 - Not used dcbData2 = 0 - Not used. dDataO = fWaitForKey (0 or 1) 0 - Return with error if no key waiting 1 - Wait for Key dDatal = 0 - Not used dData2 = 0 - Not used H Notify On Global Keys The Notify On Global Keys function (2) allows a program to look for any keystroke entered with the Ctrl and Alt keys depressed. This allows for “hot-key” operation. Unlike regular keystrokes, keys that are pressed when the Ctrl-Alt keys are pressed are not buffered. This means that users of the Read Keyboard function (1) will not see these keys. It also means that your application must have an outstanding Notify request with the keyboard service to receive these keys. If you have an outstanding Notify request and you no longer want to receive Global key notifications, you should send a Cancel Notify request (service code 3). The Global key codes returned are identical to the regular key codes, described in detail later. These are the parameters for the Notify On Global Keys function: wSvcCode = 2 npSend = 0 pDatai = Pointer where the KeyCode will be returned dcbDatai = 4 - Count of bytes in the code
I
192
Part III ■ MMURTL-Specific Details
pData2 = 0 - Not used
dcbData2 = 0 - Not used
dData0 = 0 - Not used
dDatal = 0 - Not used
dData2 = 0 - Not used
Cancel Notify On Global Keys
The Cancel Notify On Global Keys function (3) cancels an outstanding Notify On Global
Keys request. If you have an outstanding Notify request, this function cancels it. The Notify
request is sent back to the exchange with an error stating it was canceled. The Cancel Notify
request is returned also. This cancels ^// outstanding Notify requests from the same job num-
ber for your application.
Here are the parameters for Notify On Global Keys function:
wSvcCode = 3
npSend = 0
pDatai = 0 - Not used
dcbDatai = 0 - Not used
pData2 = 0 - Not used
dcbData2 = 0 - Not used
dDataO = 0 - Not used
dDatal = 0 - Not used
dData2 = 0 - Not used
Assign Keyboard
The Assign Keyboard request assigns a new job to receive keys from the service. This request
should be used only by services such as the Monitor or a program that manages multiple jobs
running under the MMURTL OS.
These are the parameters for the Assign Keyboard request:
wSvcCode = 3
npSend = 0
pDatai = 0 - Not used
dcbDatai = 0 - Not used
pData2 = 0 - Not used
dcbData2 = 0 - Not used
dDataO = x - New Job Number (1 to njobs)
dDatal = 0 - Not used
dData2 = 0 - Not used
Chapter 13 ■ Keyboard Se Key Codes and Status MMURTL supports the standard AT 101-key advanced keyboard. All ASCII text and punc- tuation is supported directly, while providing complete keyboard status to allow further trans- lation for all ASCII control codes. Alphanumeric Key Values The key code returned by the keyboard service is a 32-bit (4-byte) value. The low-order byte is the actual key code, as shown in Table 13.4. To eliminate all the key status from the 4-byte key code to get the keystroke value itself, logi- cally AND the key code by OFFh (Oxffin C). This leaves you with the 8-bit code for the key. The key will be properly shifted according to the state of the Shift and Lock keys. The upper three bytes provide the status of special keys (Shifts, Locks, and so on). The second byte provides the shift state, which is six bits for Shift, Alt, and Ctrl, as shown in Table 13.2. The third byte is for lock states (three bits for Caps, Num, and Scroll), as shown in Table 13.3. The high-order byte is for the Numeric Pad indicator and is described later. Shift-State Byte (Second Byte) If you need to know the shift state of any of the shift keys (Ctrl, Alt, or Shift), you can use the second byte in the 32-bit word returned. Table 13.2 shows how the bits are defined. Table 13.2. Shift-state bits.
Bit Meaning When Set (1) 0 Left Ctrl key down 1 Right Ctrl key down 2 Left Shift key down 3 Right Shift key down 4 Left Alt key down 5 Right Alt key down 6 Not used (0) 7 Not used (0) The following masks in assembly language can be used to determine whether the Ctrl, Shift, or Alt keys were pressed for the key code being read.
jcJT] Part III ■ MMURTL-Specific Details CtrlDownMask EQU 00000011b ShftDownMask EQU 00001100b AltDownMask EQU 00110000b Lock-State Byte (Third Byte) If you need to know the lock state of any of the lock-capable keys (Caps, Num, or Scroll), you can use the third byte in the 32-bit word returned. Table 13.3 shows how the bits are defined: Table 13.3. Lock-state bits.
Bit Meaning When Set (1) 0 Scroll Lock On 1 Num Lock On 2 Caps Lock On 3 Not used (0) 4 Not used (0) 5 Not used (0) 6 Not used (0) 7 Not used (0) The following masks in assembly language can be used to determine whether one of the lock keys was active for the key code being read. CpLockMask EQU 00000100b NmLockMask EQU 00000010b ScLockMask EQU 00000001b Numeric Pad Indicator Only one bit is used in the high-order byte of the key code. This bit (Bit 0, LSB) will be set if the keystroke came from the numeric keypad. You need this if you use certain keys from the numeric pad differently than their equivalent keys on the main keyboard. For example, in your program, the Enter key on the numeric keypad might do something differently than the type- writer Enter key. Key Codes Table 13.4 shows the hexadecimal value provided in the low-order byte of the key code (U.im significant) by the keyboard service for each key on a 101 -key keyboard. The Shift Code
Chapter 13 M Keyboard Service ra column shows the value returned if the Shift key is active. The Caps Lock key does not affect the keys shown with an asterisk (*) in the Shift Code column. A description is given only if required. If a shift code is not shown, the same value is provided in all shifted states (Shift, Ctrl, Alt, or Locks). All codes are 7-bit values (1 to 127 — lh to 7Fh). Zero and values above 127 will not be returned. Table 1 3.4. Key values. Base Key Desc. Key Code Shifted Key Shift Code Desc Esc Escape lBh 1 31h 1 21h exclamation 2 32h @ 40h at 3 33h
Equal 3Dh + 2Bh BS Backspace 08h HT Tab 09h a 61h A 4lh b 62h B 42h c 63h C 43h d 64h D 44h e 65h E 45h f 66h F 46h g 67h G 47h h 68h H 48h i 69h I 49h J 6Ah J 4Ah
7Eh tilde CR Enter ODh ; Semicolon 3Bh 3Ah colon ' Apostr. 27h " 22h quote , Comma 2Ch < 3Ch less Period 2Eh
3Eh greater / Slash 2Fh 3Fh question Space 20h Function Strip Key Codes Shift and Lock states do not affect the function key values. Shift and Lock state bytes must be used to determine program functionality. Table 13.5 shows the key code value returned.
Chapter 13 M Keyboard Service
|
197
Table 13.5. Function key strip codes.
Base Key
Key Code
Fl
F2
F3
F4
F5
F6
F7
F8
F9
F10
Fll
F12
OFh
lOh
llh
12h
13h
I4h
15h
16h
17h
18h
19h
lAh
Numeric Pad Key Codes
The Shift values shown in Table 13.6 are returned when the shift keys or Num Lock are active.
Caps Lock does not affect these keys.
Table 13.6. Numeric pad key codes.
Base Key
Key Code
Shift Code
Shifted Key
End
OBh
31h
1
Down
02h
32h
2
PgDn
OCh
33h
3
Left
03h
34h
4
Blank key
lFh
35h
5 (Extra Code)
Right
04h
36h
6
Home
06h
37h
7
Up
Olh
38h
8
PgUp
05h
39h
9
Insert
OEh
30h
0
Delete
7Fh
2Eh
. (Period)
continues
19g Part III ■ MMURTL-Specific Details Table 13.6. continued Base Key Key Code Shift Code Shifted Key CR Dash 2Dh Asterisk 2Ah Slash 2Fh Plus 2Bh Enter ODh Cursor, Edit, and Special Pad Key Codes None of the keys in these additional keypads are affected by Shift or Lock states. The values returned as the key code are shown in Table 13.7. Table 13.7. Additional key codes. Base Key Key Code Print Screen ICh Pause lDh Insert OEh Delete 7Fh Home 06h End OBh PgUp 05h PgDn OCh Up Olh Down 02h Left 03h Right 04h Your Keyboard Implementation You may not want all of the functionality in your system that I provided, or you may wan more, such as international key translations. The keyboard translation tables could be used to nationalize this system, if you desire.
Chapter 13 M Keyboard Service
You may also want the keyboard more deeply embedded in your operating system code. Chap-
ter 25, “Keyboard Code,” contains the source code behind the system service described in this
chapter. Pieces of it may be useful for your system.
I also recommend that you concentrate on the documentation for your keyboard implementa-
tion, as I have tried to do in this chapter. Remember, when all else fails, the programmer will
read the documentation.
[
199
Powered by TurnKey Linux.