accelerator keys in win32 API

This is a discussion on accelerator keys in win32 API within the Windows Programming forums, part of the Platform Specific Boards category; ive tried long and hard to get accelerator keys working, but its proved very difficult. i just cant see what ...

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    accelerator keys in win32 API

    ive tried long and hard to get accelerator keys working, but its proved very difficult. i just cant see what im doing wrong.

    i created an accelerator table with the MSVC resource editor. i added an accelerator. i defined the IDs. i added code before the message loop to load the table, and modified the message loop to translate accelerator messages. then i set up a capture in WM_COMMAND for the ID of the key. but nothing happens, no message is sent at all when i press the accelerator key. what could be wrong?

    Code:
    IDR_ACCELERATOR1 ACCELERATORS DISCARDABLE 
    BEGIN
        "Z",            ACCEL_QUIT,             VIRTKEY, CONTROL, ALT, NOINVERT
    END
    Code:
    if ( (table=LoadAccelerators(hInstance,
        (LPCTSTR)MAKELONG(MAKEINTRESOURCE(IDR_ACCELERATOR1),0)))==NULL)
    	        exit(500);
    	
    
        // Step 3: The Message Loop
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
    		if(!TranslateAccelerator(Msg.hwnd, table, &Msg))
    		{
    			TranslateMessage(&Msg);
    			DispatchMessage(&Msg);
    		}
        }
    Code:
    //these are at the very top of the source file
    #define ACCEL_QUIT 10032
    #define IDR_ACCELERATOR1 10033
    Code:
    case WM_COMMAND:
        {
              switch(LOWORD(wParam))
                    {
                           case ACCEL_QUIT:
    		MessageBox(hwnd,"Quit","!",MB_OK);
    		PostQuitMessage(0);				return 0;
    thats all the code involved. are there any glaring errors there, or anything im doing wrong?
    Last edited by bennyandthejets; 11-08-2002 at 10:26 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You are using LoadAccelerators incorrectly, this is how it should be done:
    Code:
    LoadAccelerators (hInstance, MAKEINTRESOURCE (IDR_ACCELERATOR1));
    This is as described in msdn for the API fn:
    lpTableName
    Pointer to a null-terminated string that names the accelerator table to load. Alternatively, this parameter can specify the resource identifier of an accelerator-table resource in the low-order word and zero in the high-order word. The MAKEINTRESOURCE macro can be used to create this value.
    So you could rename your accelerator resource "IDR_ACCELERATOR" (with the quotes), remove the #defined numerical value for IDR_ACCELERATOR and:
    Code:
    LoadAccelerators(hInstance,TEXT("IDR_ACCELERATOR"));
    as an alternative. You can, of course, give it any name you like.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    i tried both those methods and nothing worked. with the first method, i received a NULL return from the LoadAccelerators function.
    in the second method it seemed that the accelerator table loaded okay, but nothing happened when i pressed the accelerator. i then went into Spy++ and found this:

    Code:
    <02022> 00000FC4 S WM_COMMAND wNotifyCode:1 (sent from accelerator) wID:40001
    i thought maybe it was sending 40001 as the id of the accelerator, but nothing came of that thought. is there still something that im doing wrong?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 API or Win32 SDK?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 03:26 PM
  2. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 06:46 PM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM
  5. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 09:09 AM

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