home *** CD-ROM | disk | FTP | other *** search
- ' KeyCodes.BAS Gary D. Moore 911026
- '
- ' Example code for QuickBASIC 4.5 and PDS 7.1 to determine specific Key,
- ' or combination of Keys pressed. KeyCodes.BAS can be run in QB or QBX.
- '
- ' I have created the "subs" to show how easy it is to adapt this code
- ' to your programs. It is NOT a sample of "proper" coding. It is a
- ' sample of how you can use certain key presses (or combinations) in your
- ' code.
- '
- ' WARNING: Ctrl+F11, Ctrl+F12, Alt+F11, Alt+F12 CRASHED 'QBX' and caused
- ' an 'error' in my KeyCodes.EXE. Therefore, DO NOT USE THESE
- ' in your programs.
-
- 'My "normal" opening
- CLEAR
- CLOSE
- WIDTH 80
- SCREEN 0
- KEY OFF
- LOCATE , , 0 'turn-off cursor
-
- 'UnRemark this if you want to write a text file of Key Presses
- 'F1% = FREEFILE: OPEN "KEYS.TXT" FOR OUTPUT AS F1%: WFile% = 1
-
- Clr$ = SPACE$(79) 'Easy way to clear a Line
-
- BProg:
- COLOR 7, 0
- CLS
- COLOR 14, 0
- L$ = "Key (Scan) Codes Example Program by Gary D. Moore 1991"
- Rw% = 1: GOSUB CLn
- COLOR 7, 0
- L$ = "Press a Key, Function Key, Cursor Key, or Combination of Keys"
- Rw% = 3: GOSUB CLn
-
- NGetKey:
- Key$ = ""
- X$ = ""
- COLOR 7, 0
- LOCATE 5, 1: PRINT Clr$;
- LOCATE 23, 1: PRINT Clr$;
- 'clear the line for F1 key press in this demo
- IF X% = 15104 THEN LOCATE 10, 1: PRINT Clr$;
-
- COLOR 14, 0
- LOCATE 5, 2: PRINT "---->"
- COLOR 7, 0
-
- 'wait for a key press
- DO
- X$ = INKEY$
- LOOP UNTIL X$ <> ""
-
- 'The CVI command converts a two-byte string to an interger value
- X% = CVI(X$ + CHR$(0)) 'Determine "scancode" from Key(s) press(ed)
-
- 'Determine if user wants to quit
- IF X% = 27 THEN
- L$ = "Do you want to quit (Y/N)?": GOSUB CLn
- LOCATE 23, CN%: PRINT L$;
- Q$ = INPUT$(1)
- Q$ = UCASE$(Q$)
- LOCATE 23, 1: PRINT Clr$;
- IF ASC(Q$) = 89 THEN CLOSE : END
- END IF
-
- GOSUB KeyPress
-
- 'show key press
- COLOR 15, 0
- LOCATE 5, 9
- PRINT "You pressed ";
- 'color the Key$
- COLOR 14
- PRINT Key$
- COLOR 15
- 'some keys Line Feed the Scan Code, so force it to display on same line
- LOCATE 5, 40
- PRINT USING " Scan Code: ######"; X%
-
- IF WFile% = 1 THEN 'Write to FILE if UnRemarked above
- PRINT #F1%, "Key: "; Key$;
- PRINT #F1%, TAB(20); USING " Scan Code: ######"; X%
- END IF
-
- 'A timer routine could also be used instead
- COLOR 14, 0
- L$ = "Press <Enter> to Continue": GOSUB CLn
- LOCATE 23, CN%: PRINT L$;
- X$ = INPUT$(1)
- X$ = ""
- IF X% = 12 GOTO BProg 'Ctrl-L is Form Feed -- it clears the screen
- GOTO NGetKey 'Loop back for another key press (a do-loop would work here)
-
- 'Subs ********
-
- 'Center the Line
- CLn:
- CN% = 40 - (LEN(L$) / 2)
- IF Rw% THEN LOCATE Rw%, CN%: PRINT L$
- Rw% = 0
- RETURN
-
- ' A 'few' of the multitude of Key Presses.
- ' This program can be 'run' from inside QBX (PDS 7.1), therefore,
- ' other scancodes can be added to this list with ease (i.e. Alt+Character,
- ' Ctrl+Character, etc. and etc.)
- KeyPress:
- SELECT CASE X%
- 'Function keys
- CASE 15104
- Key$ = "F1"
- COLOR 0, 7
- L$ = " You pressed the F1 Key ": Rw% = 10: GOSUB CLn
- COLOR 7, 0
- CASE 15360: Key$ = "F2"
- CASE 15616: Key$ = "F3"
- CASE 15872: Key$ = "F4"
- CASE 16128: Key$ = "F5"
- CASE 16384: Key$ = "F6"
- CASE 16640: Key$ = "F7"
- CASE 16896: Key$ = "F8"
- CASE 17152: Key$ = "F9"
- CASE 17408: Key$ = "F10"
- 'The negatives makes it EASY to code, e.g. IF X% < 0 THEN 'do something
- ' But, not ALL key boards have these.
- CASE -31488: Key$ = "F11"
- CASE -31232: Key$ = "F12"
- 'Cursor keys
- CASE 18176: Key$ = "Home"
- CASE 18432: Key$ = "Up Arrow " + CHR$(24)
- CASE 18688: Key$ = "Page Up"
- CASE 19200: Key$ = "Left Arrow " + CHR$(27)
- CASE 19712: Key$ = "Right Arrow " + CHR$(26)
- CASE 20224: Key$ = "End"
- CASE 20480: Key$ = "Down Arrow " + CHR$(25)
- CASE 20736: Key$ = "Page Down"
- CASE 20992: Key$ = "Ins"
- CASE 21248: Key$ = "Del"
- 'this is a very unique press, but Num Lock has to be OFF
- CASE 19456: Key$ = "No. 5 on Key Pad"
- 'Shift + Function key
- CASE 21504: Key$ = "Shift+F1"
- 'Alt + Character key SEE *WARNING* at top about Alt+F11 & Alt+F12
- CASE 4096: Key$ = "Alt+Q"
- CASE 7680: Key$ = "Alt+A"
- CASE 11264: Key$ = "Alt+Z"
- CASE 11520: Key$ = "Alt+X"
- CASE 26624: Key$ = "Alt+F1"
- CASE 30720: Key$ = "Alt+1"
- 'Ctrl + Character key can be confusing, so use these for 'special' cases
- CASE 1: Key$ = "Ctrl+A"
- ' thru
- CASE 26: Key$ = "Ctrl+Z"
- 'Crtl + Function key SEE *WARNING* at top about Ctrl+F11 & Ctrl+F12
- CASE 24064: Key$ = "Ctrl+F1"
- 'Normal keys
- CASE 8: Key$ = "BackSpace"
- CASE 9: Key$ = "Tab"
- CASE 13: Key$ = "Enter"
- CASE 27: Key$ = "Esc"
- CASE 32: Key$ = "Space"
- CASE 3840: Key$ = "Shift Tab"
- 'Display ASCII characters associated with a Key Press for 'normal' keys
- CASE ELSE: IF X% < 256 THEN Key$ = CHR$(X%) ELSE Key$ = " "
- END SELECT
- 'show Ctrl Key combinations w/ ASCII character
- IF LEN(Key$) > 1 AND X% < 256 AND X% > 0 THEN Key$ = Key$ + " "+CHR$(X%)
- RETURN
-