Thread: Doing stuff when certain Key combo's pushed

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    22

    Question Doing stuff when certain Key combo's pushed

    I'm sure this is easy for someone, so here goes. I have a dialog based app that accepts use input. When a user hits something like alt and 2 at the same time, I want to change all the text to * or something. How do I check for the key combo being pressed?
    -HisWord

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    When processing for the WM_KEYDOWN message for 2 you call the GetKeyState() function with VK_MENU (the virtual key for alt) with as its parameter to check if alt was being pressed.
    zen

  3. #3
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    you could add an accelerator table to your resource script.
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, alt + combiantions are easy, because there's a special message sent when a key is pressed with ALT.

    That is the WM_SYSKEYDOWN message -- you could trap this, or trap WM_SYSCHAR, and if the character is '2' then do what you want to do.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    22

    Talking

    Thanks guys this is a lot of good information for me to research. If anyone has some sample code as to how to do this that would be great. I'll be looking today for some. Thanks again.
    I can't say for sure.....but that doesn't look work related.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    Ok guys, I've been looking around and found some sample code and tried this, but it doesn't work.

    switch(uMsg){

    case WM_SYSKEYDOWN:
    case WM_SYSKEYUP:

    switch(wParam){

    case VK_F1:
    for(j=0;j<700;j++){
    PassTable[j]=0x2a;
    }
    hwndInput=GetDlgItem(hDlg,IDC_INPUT);
    SetWindowText(hwndInput,PassTable);
    break;
    }
    break;
    break;

    Now this is dialog based, would that have anything to do with it??
    I can't say for sure.....but that doesn't look work related.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    That looks like it should do something, both when you press ALT+f1, and when you release it.

    Are you sure that the window you're using has the keyboard focus? In some programs, it can be tricky to know which has focus, in cases where your main window is a frame window, and the window you are interacting with is its client window. In this case, the client window will get the messages.

    I'm attaching some of my own code, this DOES work. I hope it helps.

    Code:
     switch (msg)
        {
            // other nonrelevant stuff removed for clarity
           case WM_SYSKEYDOWN:
                if (wParam == 'F') s.showFPS = !s.showFPS;
            case WM_KEYDOWN:
                // Handle any non-alt combo key commands
                switch (wParam)
                {
                    case VK_F1:
                        PlaySound("audio\\hailduke.aud", 0, SND_ASYNC |  SND_NODEFAULT| SND_FILENAME);
                        break;
                    case VK_F2:
                        MIDI->Play("audio\\theme.mus");
                        break;
                    case VK_F3:
                        MIDI->Play("audio\\loop.mus",true);
                        break;
                    case VK_F4:
                        MIDI->Stop();
                        PlaySound("",0,SND_ASYNC);
                        break;
                    case VK_F12:
                        absolutelyUpdate = true;
                        break;
                    case VK_SPACE:
                        spaceIsDown = true;
                        break;
                }
                break;
            case WM_SYSKEYUP:
            case WM_KEYUP:
                //handle key release commands
                switch(wParam){
                    case VK_SPACE:
                         spaceIsDown = false;
                         break;
                    case VK_F12:
                    		absolutelyUpdate = false;
                         break;
                }
                break;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Crap, it messed up some of the tabs, but it still looks readable.

    This, in a nutshell, does the following:

    F1 plays a WAV, F2 and F3 play MIDIs. F4 stops playback. The program tracks whether the spacebar is down or up, and when the alt+f combo is pressed, it toggles showing or hiding FPS. As long as f12 is down, it forces total screen redraws ever 1/30th of a second.

    This code was basically done to provide a quick means of testing various things (tests MIDIs, WAVs, and forced screen redraw vs. "intelligent" screen redraw).

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    Getting Char Messages on Dialog Apps

    Funny you ask. I had the same problem a while back. Try this:
    PreTranslateMessage is a virtual function that you must add trough the class wizard. This works I just tried it.


    BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
    {
    if(pMsg->message == WM_KEYDOWN)
    {
    //do this or the other

    MessageBox("Aha!");

    return TRUE;

    }

    return CDialog::PreTranslateMessage(pMsg);
    }

  10. #10
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    add these files to a Win32 project. build it and see what they do. hopefully you'll understand from the code. if not just repost.
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    V, I get what your code is doing, which is basically how mine is set up, but my app is a dialog, not a window. Zman, I'm using Borland free command line deal so there is no wizard. Any other suggestions??
    I can't say for sure.....but that doesn't look work related.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Well, a dialog IS a window. It's a style of window, but anything which works on a generic window will work on it.

    You're using the Borland compiler -- are you using OWL (is this even supported for the free compiler?) or are you using WinAPI command to create/handle windows?

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    I'm just using WinAPI. I don't know if it supports OWL or not, never had to use it. I was thinking that it should worl on dialogs too, but in every example I've seen so far, it works on a dialog that was first created from an app that was a window. By window I mean the whole WNDCLASSEX WndClass; thing. I even tried the accelerator keys, but that didn't work either.
    I can't say for sure.....but that doesn't look work related.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    22

    Talking

    Ok. With much messing around and rereading all the info you guys have provided, I finally was able to get the Dialog to do what I want, sorta. Here's the whole deal. My dialog box accepts user input, through an editbox. If I use SetFocus(hDlg) so that all the WM_SYSKEYDOWN stuff works, I can't type. I guess cause just the dialog has the focus and not the editbox??? So, If while I'm typing in the editbox, and I hit alt-f1, I want it to do something. What do you guys think??
    I can't say for sure.....but that doesn't look work related.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Catching key strokes in a ListView
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 07-28-2006, 02:40 AM
  2. Tab key stuff. C+WinAPI is killing me. Please help.
    By Templario in forum Windows Programming
    Replies: 5
    Last Post: 11-21-2002, 03:35 PM
  3. key problem
    By madsmile in forum Game Programming
    Replies: 20
    Last Post: 06-05-2002, 10:57 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM