Thread: keyboard accelerator problems

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Unhappy keyboard accelerator problems

    hello everybody !

    I have a problem to add keyboard accelerators to my WIN32 (no MFC) application. I did all the API documentation said but does not work !!

    here is an extract of the code i am using in the main function of the application:


    //******************************************

    ACCEL tabla[2]; // array for store accelerator info
    HACCEL h_tabla; // handle for accelerator table

    // load accelerator info
    tabla[0].fVirt = FVIRTKEY;
    tabla[0].key = VK_F5;
    tabla[0].cmd = 1001;

    tabla[1].fVirt = FVIRTKEY;
    tabla[1].key = VK_F6;
    tabla[1].cmd = 2001;

    // create accelerator table
    h_tabla = CreateAcceleratorTable(tabla,2);


    while (GetMessage(&msg, (HWND) NULL, 0, 0)) {

    if (!TranslateAccelerator(hwndMain, h_tabla,&msg)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }

    //*******************************

    when i press F5 or F6 nothing happen, through i capture de WM_COMMAND message. i try to do this using the function LoadAccelerators and an accelerator table resource and worked OK !! so the problem is with the run-time way, isn't it? and i need to do it with out resources !!


    p.d. Sorry about my english

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try checking the HACCEL returned is not NULL (if it is GetLastError() )

    or try

    &(tabla[0]) rather than tabla
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Unhappy still with problems

    i try you idea, but still does not work

    i am attaching an example program (do not worry, is small), if you can read it to find the clue of the problem i will apreciate it.

    :-)


    // ***** the program


    #include <windows.h>

    LRESULT APIENTRY MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

    HINSTANCE g_hInst;

    HWND testwin;

    HMENU principal;

    HICON d;

    HACCEL h_hotkey;

    ACCEL hotkey[2];

    // ***************** WINMAIN ****************************

    int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR line,int CmdShow) {

    g_hInst = hInst;
    MSG msg;
    WNDCLASS wc;

    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = NULL;
    wc.hInstance = hInst;
    wc.hIcon = NULL; //LoadIcon(hInst,MAKEINTRESOURCE(IDI_ICON1));
    wc.hCursor = LoadCursor(NULL,IDC_ARROW);
    wc.lpfnWndProc = (WNDPROC)MainWndProc;
    wc.lpszClassName = "jlb";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    RegisterClass(&wc);

    testwin = CreateWindowEx(WS_EX_CLIENTEDGE,"jlb","YaSP SOFTWARE 2002",WS_OVERLAPPEDWINDOW,0,0,800,600,0,0,hInst,0) ;

    hotkey[0].fVirt = FVIRTKEY;
    hotkey[0].key = VK_F1;
    hotkey[0].cmd = 1001;

    hotkey[1].fVirt = FVIRTKEY;
    hotkey[1].key = VK_F2;
    hotkey[1].cmd = 1002;

    // if i use the nect line everithing works ok !!
    // h_hotkey = LoadAccelerators(hInst,MAKEINTRESOURCE(1));

    /* here is the resource script for the accelerators

    luciano ACCELERATORS
    {
    VK_F1, 1001, VIRTKEY
    VK_F2, 1002, VIRTKEY
    }

    */

    h_hotkey = CreateAcceleratorTable(&(hotkey[0]),2);

    if (h_hotkey == NULL) return 0;

    ShowWindow(testwin,SW_SHOWMAXIMIZED);
    UpdateWindow(testwin);

    while(GetMessage(&msg,0,0,0)) {
    if (! TranslateAccelerator(msg.hwnd,h_hotkey,&msg)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }

    return msg.wParam;
    }

    // ***************** WINMAIN ****************************


    // ***************** PROC ****************************

    LRESULT APIENTRY MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
    HDC dc;
    PAINTSTRUCT ps;
    HBRUSH pincel,pincel2;
    RECT area;
    int divi;

    MENUITEMINFO itmenu;

    HMENU hMenu;

    char teclado[KL_NAMELENGTH];

    switch(msg) {

    case WM_PAINT: {

    pincel = CreateSolidBrush(RGB(110, 0, 255));
    pincel2 = CreateSolidBrush(RGB(255, 255, 255));
    dc = BeginPaint(hwnd, &ps);

    GetClientRect(hwnd, &(ps.rcPaint));

    divi = ps.rcPaint.bottom / 3;
    area = ps.rcPaint;
    area.bottom = divi;
    FillRect(dc, &area , pincel);
    area.top = divi;
    area.bottom = divi * 2;
    FillRect(dc, &area, pincel2);
    area.top = divi*2;
    area.bottom = divi * 3;
    FillRect(dc, &area, pincel);
    EndPaint(hwnd, &ps);

    }

    break;

    case WM_DESTROY: {
    DestroyAcceleratorTable(h_hotkey);
    DestroyMenu(principal);
    PostQuitMessage(0);
    }
    break;

    case WM_CREATE:{
    principal = CreateMenu();

    // OPCIONES DEL MENU PRINCIPAL

    itmenu.cbSize = sizeof(MENUITEMINFO);

    itmenu.fMask = MIIM_DATA | MIIM_ID | MIIM_STATE | MIIM_TYPE ;
    itmenu.fType = MFT_STRING;
    itmenu.fState = MFS_ENABLED;
    itmenu.wID = 1001;
    itmenu.dwTypeData = "Opción &Uno";
    itmenu.cch = 10;
    InsertMenuItem(principal,0,true,&itmenu);


    itmenu.fMask = MIIM_DATA | MIIM_ID | MIIM_STATE | MIIM_TYPE ;
    itmenu.fType = MFT_STRING;
    itmenu.fState = MFS_ENABLED;
    itmenu.wID = 1002;
    itmenu.dwTypeData = "Opción &Dos";
    itmenu.cch = 11;
    InsertMenuItem(principal,2,true,&itmenu);

    // FIN OPCIONES DEL MENU PRINCIPAL

    SetMenu(hwnd,principal);

    }
    break;

    case WM_COMMAND: {

    switch(LOWORD(wParam)) { // SWITCH BOTONES

    case 1001: {
    GetKeyboardLayoutName(teclado);
    MessageBox(hwnd,teclado,"AVISO",MB_OK);
    }
    break;

    case 1002: {
    MessageBeep(MB_OK);
    MessageBox(hwnd,"OPCIÓN DOS","AVISO",MB_OK);

    }
    break;
    } // SWITCH BOTONES

    }
    break;

    default: return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;
    }
    // ***************** PROC ****************************

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    try doing this:
    Code:
    h_hotkey = CreateAcceleratorTable((LPACCEL)hotkey,2);
    
    //instead of
    
    h_hotkey = CreateAcceleratorTable(&(hotkey[0]),2);

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    i tried this:

    h_hotkey = CreateAcceleratorTable((LPACCEL)hotkey,2);


    but does not work again.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I just compiled your source unchanged with MS VC++ 6.0 Pro, and as far as I can see, it works...

    If I click the menu item or press F1 I get the same dialog box "00000406", or clicking the second or pressing F2 I get the dialog box "OPCION DOS".

    What is your problem/environment?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    ohhhhhh my god !!

    you are completely right adrianxw, i was compiling the program with Borland C++ 5.01 and does not work !! but when i compile it with VC++ 6 everything works fine !!

    in the other hand, why Borland c++ works bad ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 04:24 AM
  2. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 02:46 PM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  4. What type of keyboard is this?
    By 7smurfs in forum Tech Board
    Replies: 5
    Last Post: 06-22-2005, 05:09 PM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM