Getting "Alt" to work

Hey guy,

I have a basic Hello World Application I'm writing.

As of now I capture single keystrokes with:

do {} while(!kbhit());
char keystroke = _getch();

Then I process the keystroke with a switch statement:

switch(keystroke) {
case 'q': { cout << "Now quiting!"; return 0; }
default: { cout << "Try another key" << endl; break;
}

My question is: "How do I capture/test if Alt has been pressed?
Thank you in advance
Getting closer, here is some code that recognized when Alt and a letter is pressed.
Don't forget to include: #include <windows.h>

printf("%d, is Esc\n", VK_ESCAPE);
printf("%d, is Tab\n", VK_TAB);
printf("%d, is Alt(Menu\n", VK_MENU);

int ch;

do {
do{}while(!kbhit());

ch = _getch();
if(GetAsyncKeyState(VK_MENU)) printf("You hit Alt!!!\n");
printf("%i", ch);
if (ch == 0 || ch == 224) printf (", %d", _getch());
printf("\n");
} while(ch != 27);
Last edited on
I'm on a linux and it's been about 4 years since I touched winAPI, but I'd suggest you look into getKeyState() and virtual key codes on msdn.

You might also look into SDL or Qt keyboard events since those libraries are cross-platform (run on windows, mac, android, linux, etc.) but that's kind of a personal choice that I shouldn't push you about...


https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301(v=vs.85).aspx -- getkeystate()
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx -- VK list
Last edited on
Yah, been on there for a while, it's all very cryptic for me. Although, I'm making progress, but it's ridiculous how difficult this is.

The way I have it set up, when you press alt, then is spits out thousands of them per key press but all other keys it takes as a single stroke.

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <conio.h>
#include <windows.h>

int main(void) {

    printf("%d, is Esc\n", VK_ESCAPE);
    printf("%d, is Tab\n", VK_TAB);
    printf("%d, is Alt(Menu)\n", VK_MENU);

    printf("Press ESC to exit\n");
    int ch, myBreak;
    myBreak = 0;

    do {
        do{
            if(GetAsyncKeyState(VK_MENU)) {
                printf("You hit Alt!!!\n");
                myBreak=1;
            } if(myBreak==1) break;
        }while(!kbhit());
        myBreak=0;
        if(GetAsyncKeyState(VK_MENU)) { printf("Alt still pressed"); } else
            ch = _getch();
        printf("%i", ch);
        if (ch == 0 || ch == 224) printf (", %d", _getch());
        printf("\n");


    } while(ch != 27);

    printf("ESC %d Pressed\nNow Exiting Application", ch);

    return 0;
}
If you are using alt as a mod/shortcut key, then you'd wait until a normal key was pressed and then check if alt is also pressed (just one check needed). Which might seem a bit strange, but that's how you would check for multiple keys pressed at once.

Most winApi programs are event-driven, which means that they are opened up, do some basic setup that is needed to properly display the window and its contents, then gives control back to the OS. The program does not have to wait though, It has stuff it can do in the mean-time, like playing a video, move enemies in the direction of the character, or just spell-check text. Those are often activated by timers. Basically. most of the functions you're going to find in WinApi will not pause, it wants the OS to maintain the in-between time.

Having said that, _getch() is meant for console apps, it does wait for the enter button to be pressed before taking input. Your entire program is paused when you use that function, which is good since you have no way to pass control to the OS at this level. Both methods are ways to use as little CPU as needed while waiting on user input.
Last edited on
Love the effort, even if it's a bit off. No harm meant by this.

Go open Notepad. press Alt, and then "f" and then 'x' That's the functionality I want. Right now I've switched over to using "Esc" as the key to press to go from the edit window to the menu bar. I know getting a single keystroke "Alt" can be done because others have done it.

Two, go look at the do {} while(!kbhit()); ... that line keeps things moving along until there is a keyboard hit, therefore _getch() doesn't get a chance to do anything until there is something there. Therefore, the system isn't locked, up. Monsters can move, food levels can drop, windows can keep leaking memory like screen door, etc.

Third, go up and read my first line, this is a C++ Hello World app, therefore, it's all console.

I will get into using mouse functionality on the console screen, I'll get multithread functionality into the console, I'll get databases and internet activity going in my console window, but for now, I just want to get a single keystroke to the Alt key to get me into the menu bar without have to have another key being pressed to get Alt to do what it needs to.

Topic archived. No new replies allowed.