How do I trap a key?

Hello, world! This is Dr. COM WIZ. I am currently in the middle of mass game development, but have reached yet another road block!

While animation is in session in my game, there are some points where the user has to "Press this to do this. Press that to do something else." In my game, the user usaully has to hold the key, so I had to change it to "HOLD this key to do that." I am seriously serious! I tried to help that by:

1) Sticking lot's of IF INKEY$ statements, but too many will cause QBasic to act really funny.

2) Replacing the letter in the quotes with their ASCII code, but that doesn't work at all. Why, by the way?

So, how do I do it? How do I trap user-defined keys like "l" or ESC for instant response?

If you forgot how to do that, or if you just feel like answering another question, here is one: How do I make one of those menus where you use the arrow keys to scroll down the options?
This has been,
Dr. COM WIZ
PS Thank you and goodnight!

Comments

  • To really know what going on I'd need to look at some code, but it sounds like you've hit some speed problems. There are ways to speed up a program. QB is faster than most people think, If you use integers. Much of QB speed problems are because many QB programers use floats for everything. Here's an example:
    [code]
    FOR i = 1 to 1000
    NEXT i
    [/code]
    When you initialize a variable QB needs to know what type it is. If you name a variable i with no symbol after it, QB assumes you want a FLOAT. That FOR-NEXT loop uses a float variable. If you name the variable i% then QB knows that you want a 2 byte integer.
    [code]
    FOR i% = 1 TO 1000
    NEXT i%
    [/code]
    That last loop is guarateed to run faster.

    Oh yeah. QB treats i% i& i i! as 4 different variables.

    You can also DIM a variable as a certain type
    [code]
    DIM i AS INTEGER
    FOR i = 1 TO 1000
    NEXT i
    [/code]

    That may not be the problem, but I hope I've helped.




    : Hello, world! This is Dr. COM WIZ. I am currently in the middle of mass game development, but have reached yet another road block!
    :
    : While animation is in session in my game, there are some points where the user has to "Press this to do this. Press that to do something else." In my game, the user usaully has to hold the key, so I had to change it to "HOLD this key to do that." I am seriously serious! I tried to help that by:
    :
    : 1) Sticking lot's of IF INKEY$ statements, but too many will cause QBasic to act really funny.
    :
    : 2) Replacing the letter in the quotes with their ASCII code, but that doesn't work at all. Why, by the way?
    :
    : So, how do I do it? How do I trap user-defined keys like "l" or ESC for instant response?
    :
    : If you forgot how to do that, or if you just feel like answering another question, here is one: How do I make one of those menus where you use the arrow keys to scroll down the options?This has been,
    : Dr. COM WIZ
    : PS Thank you and goodnight!
    :


  • : 2) Replacing the letter in the quotes with their ASCII code, but that doesn't work at all. Why, by the way?

    You should be able to use a character's ASCII code. Are you just replacing the letter with its ASCII code, or are you using the CHR$() function? You need to use CHR$. For example, the following two examples are identical:
    [code]
    PRINT "Press 1"
    a$ = INPUT$(1)
    IF a$ = "1" THEN PRINT "1 pressed"
    [/code]
    and...
    [code]
    PRINT "Press 1"
    a$ = INPUT$(1)
    IF a$ = CHR$(49) THEN PRINT "1 pressed"
    [/code]
    I wouldn't recommend using ASCII codes unless its absolutely necessary. It makes your program hard to read.

  • : : 2) Replacing the letter in the quotes with their ASCII code, but that doesn't work at all. Why, by the way?
    :
    : You should be able to use a character's ASCII code. Are you just replacing the letter with its ASCII code, or are you using the CHR$() function? You need to use CHR$. For example, the following two examples are identical:
    : [code]
    : PRINT "Press 1"
    : a$ = INPUT$(1)
    : IF a$ = "1" THEN PRINT "1 pressed"
    : [/code]
    : and...
    : [code]
    : PRINT "Press 1"
    : a$ = INPUT$(1)
    : IF a$ = CHR$(49) THEN PRINT "1 pressed"
    : [/code]
    : I wouldn't recommend using ASCII codes unless its absolutely necessary. It makes your program hard to read.
    : Ofcourse I used the CHR$() function! I am not a beginner, you know. (I hope I wasn't being rude.) Thank you for your help, though.

    I used ASCII codes only when ESC is the "hot key." When I try to use it for visual characters like "L," "R," and " ," ASCII codes absolutley do not work. What gives?

    --Dr. COM WIZ


  • : I used ASCII codes only when ESC is the "hot key." When I try to use it for visual characters like "L," "R," and " ," ASCII codes absolutley do not work. What gives?

    It should work for any letter or punctuation mark as well, so I'm not sure why your program isn't working - I'd probably have to see a bit of code. For example, with a ",":
    [code]
    PRINT "Press , (comma)"
    a$ = INPUT$(1)
    IF a$ = CHR$(44) THEN PRINT ", pressed"
    [/code]
    One possible reason could be that you are forgeting that upper- and lower-case letters have different ASCII codes. So if your program says [italic]when a$ = ASCII code for L[/italic], it will not work if they enter a lower case L. You will need to use an LCASE$ or UCASE$ function to avoid this:
    [code]
    PRINT "Press L"
    a$ = INPUT$(1)
    IF UCASE$(a$) = CHR$(76) THEN PRINT "L or l pressed"
    [/code]
    I'm also not sure what you mean by "visual" characters. When you say "L" and "R", do you mean the left and right arrow keys or the letters L and R? In the case of arrow keys, they return two characters instead of one and the first character is a null (ie. ASCII character zero). For example:
    [code]
    CLS
    PRINT "Press any arrow keys, or escape to quit"
    DO
    a$ = INKEY$
    SELECT CASE a$
    CASE CHR$(0) + "H": PRINT "Up"
    CASE CHR$(0) + "P": PRINT "Down"
    CASE CHR$(0) + "K": PRINT "Left"
    CASE CHR$(0) + "M": PRINT "Right"
    END SELECT
    LOOP UNTIL a$ = CHR$(27)
    [/code]
    You can also use this method to capture a whole bunch of other keys, such as F1-12, Insert, Home, PgDn, PgUp, etc... Does that answer your question?



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories