Microsoft KB Archive/65925

= Using OS/2 API Calls for Keyboard Input from Basic PDS =

Article ID: 65925

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q65925



SUMMARY
This article contains a sample Basic module, which calls the OS/2 API functions to perform a simple keyboard input routine in protected mode.

This information applies to Microsoft Basic compiler versions 6.00 and 6.00b for MS OS/2 (for protected mode only) and Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS OS/2 (for protected mode only).



MORE INFORMATION
For more information on the OS/2 keyboard input functions, please refer to the &quot;Microsoft OS/2 Programmer's Reference&quot; Volume 3, Pages 164-180, published by Microsoft Press (1989).

OS/2 protected mode allows for multiple logical keyboard buffers to be set up and used by a process. When a logical keyboard buffer is opened, it does not receive any characters until that buffer is given the focus by the KbdGetFocus function. When a keyboard buffer has the focus, it receives all characters that are typed in through the keyboard.

The largest string that can be typed in is determined by the buffer that the program sets up; in this example, the buffer is set to 40. Any extra characters are ignored until the BACKSPACE, DEL, or arrow keys are pressed.

This sample program uses the KbdOpen and KbdGetFocus function to open a logical keyboard. It then uses the KbdGetStatus and KbdSetStatus functions to preserve the status of the keyboard and then to modify the status, forcing the CAPSLOCK key on. It also uses the VioSetCurType function to set the display of the cursor (by default there is no cursor).

To run the program in OS/2 protected mode, the program should be compiled and linked as follows:

BC /Lp OS2KEY; LINK OS2KEY,,,BRT71ENP.LIB+OS2.LIB;

OS2KEY.BAS
'This sample program OS2KEY.BAS uses OS/2 keyboard input routines. 'Wait flags for keyboard input and status check: CONST IOWAIT    = 0 CONST IONOWAIT  = 1 'Constant declarations for KdbInfo.fsMask: CONST KEYBOARDECHOON           = &H0001 CONST KEYBOARDECHOOFF          = &H0002 CONST KEYBOARDBINARYMODE       = &H0004 CONST KEYBOARDASCIIMODE        = &H0008 CONST KEYBOARDMODIFYSTATE      = &H0010 CONST KEYBOARDMODIFYINTERIM    = &H0020 CONST KEYBOARDMODIFYTURNAROUND = &H0040 CONST KEYBOARD2BTURNAROUND     = &H0080 CONST KEYBOARDSHIFTREPORT      = &H0100 'Constant declarations for Keyboard flags: CONST RIGHTSHIFT   = &H0001 CONST LEFTSHIFT    = &H0002 CONST CONTROL      = &H0004 CONST ALT          = &H0008 CONST SCROLLLOCKON = &H0010 CONST NUMLOCKON    = &H0020 CONST CAPSLOCKON   = &H0040 CONST INSERTON     = &H0080 CONST LEFTCONTROL  = &H0100 CONST LEFTALT      = &H0200 CONST RIGHTCONTROL = &H0400 CONST RIGHTALT     = &H0800 CONST SCROLLLOCK   = &H1000 CONST NUMLOCK      = &H2000 CONST CAPSLOCK     = &H4000 CONST SYSREQ       = &H8000 TYPE KbdInfoType   'KbdInfo structure cb          AS INTEGER fsMask      AS INTEGER chTurnAround AS INTEGER fsInterim   AS INTEGER fsState     AS INTEGER END TYPE TYPE StringInBufType   'StringInBuf structure cb   AS INTEGER cchIn AS INTEGER END TYPE TYPE StringBufType   'Fixed length string to receive input characters Chars AS STRING * 40  'Basic doesn't allow passing fixed length END TYPE                  'strings, so use a user-defined type of                           'a fixed length string. TYPE VioCursorInfoType  'Type that holds the cursor attributes yStart AS INTEGER cEnd  AS INTEGER cx    AS INTEGER attr  AS INTEGER END TYPE 'The fundamental OS/2 Keyboard functions DECLARE FUNCTION KbdOpen% (SEG hkbd%) DECLARE FUNCTION KbdClose% (BYVAL hkbd%) DECLARE FUNCTION KbdGetStatus% (SEG KbdInfo AS KbdInfoType, BYVAL hkbd%) DECLARE FUNCTION KbdSetStatus% (SEG KbdInfo AS KbdInfoType, BYVAL hkbd%) DECLARE FUNCTION KbdGetFocus% (BYVAL fWait%, BYVAL hkbd%) DECLARE FUNCTION KbdFreeFocus% (BYVAL hkbd%) DECLARE FUNCTION KbdStringIn% (SEG chBuffer AS StringBufType, _       SEG StringInBuf AS StringInBufType, BYVAL fWait%, BYVAL hkbd%) DECLARE FUNCTION VioSetCurType% (SEG VioCursorInfo AS _                                VioCursorInfoType, BYVAL hvio%) 'Dimension the structured variables DIM KbdInfo AS KbdInfoType DIM PrevkbdInfo AS KbdInfoType DIM StringInBuf AS StringInBufType DIM chBuffer AS StringBufType DIM VioCursorInfo AS VioCursorInfoType CLS ReturnVal% = KbdOpen%(hkbd%)            'Open a logical Keyboard IF ReturnVal% = 0 THEN PRINT &quot;Opened logical keyboard&quot; PRINT &quot;Getting the focus: &quot;; ReturnVal% = KbdGetFocus%(IONOWAIT, hkbd%)  'Make it the one to                                              'receive keyboard IF ReturnVal% = 0 THEN                      'input PRINT &quot;we have the focus&quot; 'Save the previous state of the keyboard, so it can be reset: PRINT &quot;Saving the previous state of the input mode&quot; PRINT &quot;Getting the status and checking for echo mode, input mode&quot; PrevkbdInfo.cb = LEN(KbdInfo) ReturnVal% = KbdGetStatus%(PrevkbdInfo, hkbd%) IF RetrunVal% = 0 THEN 'Check echo mode: IF (PrevkbdInfo.fsMask AND KEYBOARDECHOON) THEN PRINT &quot;Echo on, &quot;; ELSE PRINT &quot;Echo off, &quot;; END IF     'Check input mode: IF (PrevkbdInfo.fsMask AND KEYBOARDASCIIMODE) THEN ' PRINT &quot;Ascii mode&quot; ELSE PRINT &quot;Binary Mode&quot; END IF     'Set the cursor type: size, and attribute: VioCursorInfo.yStart = 12   'beginning scan line for cursor 'starting from top position VioCursorInfo.cEnd = 13     'ending scan line, zero-based VioCursorInfo.cx = 0        'default width, one character VioCursorInfo.attr = 0      'normal attribute, &hffff is hidden hvio% = 0                   'video handle ReturnVal% = VioSetCurType%(VioCursorInfo, hvio%) IF ReturnVal% = 0 THEN PRINT &quot;Cursor is the normal TWO scan lines tall&quot; END IF

'Initialize KbdInfo to the new status: PRINT &quot;Setting the CAPSLOCK on&quot; KbdInfo.cb = LEN(KbdInfo) KbdInfo.chTurnAround = PrevkbdInfo.chTurnAround KbdInfo.fsInterim = PrevkbdInfo.fsInterim KbdInfo.fsMask = (PrevkbdInfo.fsMask OR _   'Turn on the modify                        KEYBOARDMODIFYSTATE)       'state KbdInfo.fsState = (PrevkbdInfo.fsState OR _ 'force caps lock on                         CAPSLOCKON) ReturnVal% = KbdSetStatus%(KbdInfo, hkbd%)  'Set the status IF ReturnVal% = 0 THEN PRINT &quot;Caps lock should be on&quot; PRINT &quot;Input some characters: &quot;; StringInBuf.cb = LEN(chBuffer) 'Input the string: RetrunVal% = KbdStringIn%(chBuffer, StringInBuf, IOWAIT, hkbd%)

'During input, OS/2 does not advance the cursor, to prevent 'writing over what was typed, use a LOCATE statement, or a double 'PRINT to advance the cursor position PRINT : PRINT &quot;This is what you typed: &quot;; chBuffer.Chars SLEEP (3)                      'Sleep for three seconds ELSE PRINT &quot;Caps on failed&quot; END IF     'Start cleaning up, restore the status, free focus, and close 'keyboard: PRINT &quot;Restoring the status&quot; RetrunVal% = KbdSetStatus%(PrevKbdInfo, hkbd%) IF RetrunVal% = 0 THEN PRINT &quot;Status Returned&quot; ELSE PRINT &quot;Status Could Not Be Restored END IF

'Free the focus and close the logical keyboard PRINT &quot;Freeing the focus and closing keyboard&quot; ReturnVal% = KbdFreeFocus%(hkbd%) IF ReturnVal% = 0 THEN ReturnVal% = KbdClose%(hkbd%) IF ReturnVal% = 0 THEN PRINT &quot;Keyboard closed&quot; ELSE PRINT &quot;Keyboard could not be closed, Error= &quot;; ReturnVal% END IF     ELSE PRINT &quot;Focus could not be freed: &quot;; ReturnVal% END IF   ELSE PRINT &quot;Get status failed: &quot;; RetrunVal% END IF ELSE PRINT &quot;ERROR on Focus, ReturnVal% = &quot;; ReturnVal% END IF ELSE PRINT &quot;Logical keyboard could not be opened&quot; END IF END

Additional query words: BasicCom

Keywords: KB65925

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.