Capturing key stokes

This is a discussion on Capturing key stokes within the C++ Programming forums, part of the General Programming Boards category; Hi, is it possible to capture a key combination such as alt and f or alt and e. I've tried ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Capturing key stokes

    Hi, is it possible to capture a key combination such as alt and f or alt and e. I've tried capturing the ascii values, but only the single characters are captured and not the combination. It's basically for a menu system that i'm trying to develope, for a windows console.

    Builder 5, win 98. Thanks for any help.
    Be a leader and not a follower.

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You will probably want to write something like

    Code:
    if (GetAsyncKeyState(VK_ALT)) {
           if (GetAsyncKeyState(VK_F)) {
                       // alt-f
           }
    }

    You can't capture alt-f though with standard c++;
    this should really should be on the windows board.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    That's just what I was going to ask... is this for a Windows/console or DOS program?

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    for a menu system that i'm trying to develope, for a windows console
    Be a leader and not a follower.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Is the GetAsyncKeyState() an api function? Therefore, i'll have to include windows.h for a header file?
    Be a leader and not a follower.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Yes it is an api function, so you do have to include windows.h.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, the proper code is:
    Code:
    if (GetAsyncKeyState(VK_MENU)) {
           if (GetAsyncKeyState(36)) {
                       // alt-f
           }
    }
    You can get a list of the GetAsyncKeyState constants here.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    What return value is the if statement testing? Cos, the if the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. Therefore, shouldn’t the if condition be testing for the most significant bit using "=="?
    Be a leader and not a follower.

  9. #9
    Imperito
    Guest
    Isn't there a byte somewhere that holds ctrl, alt, shift, numlock, scrolllock, capslock, and insert? I seem to remember pokin at it in my QBASIC days.

  10. #10
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    OK now I looked it up and its the 1047th byte from DEF SEG 0.

    The bits are Insert, Caps Lock, Num Lock, Scroll Lock, Alt, Ctrl, Left Shift, Right Shift.

    So theoretically if you could load that byte into an unsigned char and then AND it against char(8) the result would tell you if the ALT key was down, and no massivesoft code required.

    Now, does anyone know how to get a specific byte from a known segment offset into an unsigned char?

  11. #11
    Nick
    Guest
    To be more proper & by 0x8000 because the windows sparse documentation only specifies that it's 0 if another process or window as keyboard focus. I suppose the bit pattern
    being 0x0110 when the key is up. I've never heard of anyone having a problem doing it the way mentioned with VK_MENU.

    for a menu system that i'm trying to develope, for a windows console
    Exactly this should be on the window's board. You could do
    this with poking at memory like in dos and have it not compile.
    My bet is that you could also do this with getch but it wouldn't handle as good.

  12. #12
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Would someone just be able to tell me what condition the if statement is testing?

    if (GetAsyncKeyState(VK_MENU))
    An if statement can only be evaluated and executed if the condition is true. The GetAsyncKeyState() function returns a short int, so how can that return be either true or false for the if statement to work.
    Be a leader and not a follower.

  13. #13
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Can no one answer the above question then?
    Be a leader and not a follower.

  14. #14
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Return Values
    If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.

    Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState with these constants, the return value is zero.

  15. #15
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    But shouldn't the function be returning a bool, e.g true or false?
    Be a leader and not a follower.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21