Thread: pushbuttons

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    pushbuttons

    I'm trying to write a calculator program and would like to know
    how to get pushbuttons to react to a key pressed. So if I press '1' the button would change. Does this require a Accelerator Table?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    This is very old and probably bug-ridden (you're just dying to use it then, right? ). If nothing else works, give it a shot....


    [code]

    char ParamsToChar(WPARAM wParam, LPARAM lParam, bool *shift_on, bool *caps_on)
    {
    char found = 0;

    int key = LOWORD(wParam);

    int value[50] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 26, 27, 28, 29, 39, 40, 41, 42, 43, 51, 52, 53, 55, 57, 58, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 284, 285, 309, 328, 331, 333, 336 };
    char unshiftTable[50] = { VK_ESCAPE, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', VK_BACK, VK_TAB, '[',']',VK_RETURN, VK_CONTROL, ';','\'','`',VK_SHIFT, '\\', ',','.','/','*', ' ', VK_CAPITAL, '7', '8','9','-','4','5','6','+','1','2','3','0','.', VK_RETURN, VK_CONTROL, '/', VK_UP, VK_LEFT, VK_RIGHT, VK_DOWN };
    char shiftTable[50] = { VK_ESCAPE, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', VK_BACK, VK_TAB, '{','}',VK_RETURN, VK_CONTROL, ':','"' ,'~',VK_SHIFT, '|', '<','>','?','*', ' ', VK_CAPITAL, '7', '7','9','-','4','5','6','+','1','2','3','0','.', VK_RETURN, VK_CONTROL, '/', VK_UP, VK_LEFT, VK_RIGHT, VK_DOWN };

    char *table = (*shift_on) ? shiftTable : unshiftTable; //...unused if we find an alphabetical...

    if(isalpha(LOWORD(wParam)) && HIWORD(lParam) < 55)
    {
    if(*shift_on || *caps_on)
    found = key;
    else
    found = tolower(key);
    }

    if(*shift_on) *shift_on = false; //...shift key expires...

    if(!found)
    {
    key = HIWORD(lParam);

    int code;

    for(code = 0; code < 50; code++)
    {
    if(value[code] == key) found = table
    Code:
    ;
             }
    
             if(found == VK_SHIFT)
               *shift_on = true;       //...shift key is reactivated...
             else
             if(found == VK_CAPITAL)
               *caps_on = !(*caps_on);
          }
    
     return found;  //...could be !found, of course...
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User Penguin of Oz's Avatar
    Join Date
    Dec 2002
    Posts
    16

    Hotkeys

    The simplest solution to your problem would be to use hotkeys in your program to detect if somebody pressed '1', '2', etc.

    This is acheived through the use of a funky function called RegisterHotKey(). Once you've registered the hotkey, you simply check for the WM_HOTKEY message in your window/dialog callback function, check the wParam for which hotkey was pressed and handle it appropriately. Here's a commented sample, fresh from the oven:

    Code:
    // First, define a hotkey ID to make life easier
    #define ID_HOTKEY_1 (WM_USER+1)
    
    ...
    
    // Now in some function (e.g. when you handle the WM_INITDIALOG message in a dialog procedure), declare this:
    BOOL bSuccess = RegisterHotKey(hWnd, ID_HOTKEY_1, NULL, VK_1);
    
    // Check if it worked
    if(!bSuccess)
    {
       MessageBox(hWnd, "Hotkey failed.", "Error", MB_OK);
       return TRUE;
    }
    
    ...
    
    // Now in your CALLBACK function, you could handle something like:
    case WM_HOTKEY:
        // First check if our window is active
        if(hWnd != GetForegroundWindow()) return TRUE;
    
        // If it is, continue
        switch(wParam)
        {
            case ID_HOTKEY_1:
                SendDlgItemMessage(hWnd, IDC_BUTTON_1, WM_LBUTTONUP, 0, 0);
            break;
            // Add more cases to handle other keys
         }
    break;
    Alright, that's pretty messy code, but hopefully it should convey some idea.

    If I was certain of how to handle the WM_KEYUP message, I would tell you that instead (it's probably much more appropriate than setting up a hotkey).

    So, if you don't want to mess about with hotkeys, look up how to handle the following messages on MSDN or something:

    * WM_KEYUP
    * WM_KEYDOWN
    * WM_CHAR

    Hope this helped a little. Sorry for the ambiguity.


    - Penguin of Oz
    "I don't think there's anything else I can do... my shoes are tied"

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Penguin of Oz, I have tried you code and have been able to compile successfully. The problem is on the with:
    BOOL bSuccess = RegisterHotKey(hWnd, ID_HOTKEY_1, NULL, VK_1);

    What's the last paramter suppose to be? A ascii value?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  5. #5
    Registered User Penguin of Oz's Avatar
    Join Date
    Dec 2002
    Posts
    16
    It's supposed to be the virtual keycode for the character '1'. Sorry, I didn't test this code... :x

    I'll try and find the VKCode for 1 now... one moment...

    Turns out you can use ASCII values there too, so replace the VK_1 with 49.
    "I don't think there's anything else I can do... my shoes are tied"

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    49 or VK_1 will be the 1 right above the Q, for the numpad, use VK_NUMPAD1

  7. #7
    if you are using MSVC++ all you have to do is, in the caption for the button put something like F&our, and it will look like this Four, and when o is pressed it presses the button,all u have to do is put & in front of the character that u want the 'hotkey' to be, make sure to 'check neumonics' somewhere in the menus,cant remember where

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    MOst likely, he wants a 4 on the button, not "Four"

    -Futura

  9. #9
    well holy crap-lol not that much different, just put &4 what was the point of your post?

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    OK, so you have an underlined 4, not too bad, but I would definitely go with hotkeys or monitoring the WM_KEYDOWN message. I'd be a cleaner solution. Also the mnemonics only work in dialog boxes, not regular windows.

    -Futura

  11. #11
    im pretty sure a calculator is going to be made in a dialog, and the nmeunoics would do perfectly fine with that, i mean, why would it be in a normal window? anyways...

  12. #12
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Well, I'm going to use Cgawd's solution for now. Maybe after I get the program fully written I'll try some of the other solutions.
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  13. #13
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    49 or VK_1 will be the 1 right above the Q, for the numpad, use VK_NUMPAD1
    When I compile using the Borland commandline compiler it says VK_1 is undefined. Shouldn't Borland be able to compile it as VK_1 instead of 49?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  14. #14
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Shouldn't Borland be able to compile it as VK_1<<

    Only if you have defined them. Msvc doesn't. From winuser.h:
    /*
    * VK_0 - VK_9 are the same as ASCII '0' - '9' (0x30 - 0x39)
    * 0x40 : unassigned
    * VK_A - VK_Z are the same as ASCII 'A' - 'Z' (0x41 - 0x5A)
    */
    So you could either use these values or #define the virtual key codes yourself.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed