Getting Key Codes

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Getting Key Codes

Post by RayBritton »

Hi, I most of the key codes, and i was wondering what the key codes for Ctrl+Left and Ctrl+Left, and how do people get them
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

keyboard scan codes

Post by Zim »

I usually use the printed reference manual, however a quick "Yahoo" search gave this as the first "hit":

http://www.barcodeman.com/altek/mule/scandoc.php
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

unforunatly that page is of no use to me, thanks anyway but i found a page that says Ctrl+Left is 0 115 00 73, how can i test what key was pressed against this?
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

Scan codes, as I recall, are either one or two bytes. "Funny" characters like Alt and Ctrl have scan codes that start with zero. So, first write a loop that scans the keyboard and checks for a key press, like:

Code: Select all

Do
   k$=inkey$
Loop until len(k$)
Then check the length of k$. If it's two instead of one then you've got one of those "special" key combos. Otherwise, you've got a "regular" one.

Code: Select all

if len(k$)>1 then special char. code else regular char code
An easy way to get the actual code is to remeber that it's either the ONLY character or the RIGHT one that you're interested in. So:

Code: Select all

Num=asc(right$(k$,1))
will give you the ascii number of the right (or only) character that was scanned.[/code]
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

if i press ctrl+left i get 115 so is that CHR$(0)+CHR$(115)
User avatar
UWLabs
Coder
Posts: 12
Joined: Sun Oct 03, 2004 7:53 am
Location: Tennessee

To find keycodes AND scan codes

Post by UWLabs »

Try this QBasic program:

Code: Select all

DECLARE SUB Center (y%, T$)
DECLARE SUB Delay (Amt!)
SCREEN 0
COLOR 9, 1
CLS
COLOR 14
Center 3, "K e y   F i n d e r   0 . 5"
COLOR 8
Center 4, "?????????????????????????????"
COLOR 7

Center 6, " This portion of the program will display the ASCII character"
Center 7, " codes for the keys you press on the keyboard.  Extended keys"
Center 8, " contain TWO characters, so both will displayed.  Combination"
Center 9, " keys like [Ctrl]+[S] will display as extended, if they are. "
Center 10, " Press the [Esc] key, (or  Ctrl + [ ), to quit at anytime."
COLOR 9, 1
Center 12, "??????????????????????????????????????????????????????????"
Center 13, "?                    ?                      ?             ?"
Center 14, "???????????????????????????????????????????????????????????"
Center 15, "??????????????????????????????????????????????????????????"
Center 16, "?                    ?                      ?             ?"
Center 17, "???????????????????????????????????????????????????????????"
COLOR 11
LOCATE 13, 13: PRINT "Primary ASCII Code"
LOCATE 13, 34: PRINT "Secondary ASCII Code"
LOCATE 13, 57: PRINT "Key Pressed"
COLOR 15

DO
  a$ = INKEY$
  IF a$ <> "" THEN
                LOCATE 16, 19: PRINT "          "
                LOCATE 16, 40: PRINT "          "
                LOCATE 16, 57: PRINT "            "
        IF LEN(a$) > 1 THEN
                b$ = STR$(ASC(LEFT$(a$, 1)))
                c$ = STR$(ASC(RIGHT$(a$, 1)))
                LOCATE 16, 22: PRINT "0"
                LOCATE 16, 42: PRINT c$
                LOCATE 16, 57: PRINT "CHR$(0)+"; CHR$(34); LTRIM$(RIGHT$(a$, 1)); CHR$(34);
        ELSE
                b$ = STR$(ASC(a$))
                i% = 0
                IF a$ = CHR$(9) THEN i% = 1
                IF a$ = CHR$(13) THEN a$ = " "
                'IF a$ = CHR$(8) THEN a$ = "BkSpc": i% = 2
                LOCATE 16, 20: PRINT b$
                LOCATE 16, 43: PRINT "--"
                LOCATE 16, 61 - i%: PRINT CHR$(34); a$; CHR$(34)
        END IF
        IF a$ = CHR$(27) THEN EXIT DO
  END IF
LOOP

Part2:
COLOR 7
Center 6, " This portion of the program will display the computer key-  "
Center 7, " board scan codes.  Press a key or key combination, and the  "
Center 8, " information will be displayed below.  Note that the codes   "
Center 9, " are different for similar keys that are placed differently. "
Center 10, " Press the [Esc] key, (or  Ctrl + [ ), to quit at anytime."
COLOR 9, 1
Center 12, "??????????????????????????????????????????????????????????"
Center 13, "?                            ?                            ?"
Center 14, "???????????????????????????????????????????????????????????"
Center 15, "??????????????????????????????????????????????????????????"
Center 16, "?                            ?                            ?"
Center 17, "???????????????????????????????????????????????????????????"
COLOR 11
LOCATE 13, 17: PRINT "Keyboard Scan Code"
LOCATE 13, 49: PRINT "Key Pressed"
COLOR 15

old% = INP(96)
DO
  a% = INP(96)
  LOCATE 16, 23: PRINT a%; "   "
  Delay .05
  a$ = INKEY$
  IF a$ = CHR$(27) THEN EXIT DO
LOOP

SUB Center (y%, T$)
i% = 40 - INT(.5 * LEN(T$))
IF i% < 1 THEN i% = 1
LOCATE y%, i%, 0
PRINT T$;
END SUB

SUB Delay (Amt!)
T! = TIMER + Amt!
WHILE TIMER < T!
WEND
END SUB

RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

thanks
Post Reply