Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

WM_KEYDOWN never happens in dialog

[code]
hi,
i want to handle the KEYDOWN event in a dialog i load through
DialogBox() functions. The problem is how ?,
-----------------------
[b]the program:[/b]
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE phInstance,LPSTR cmd,int nShow){
hInst=hInstance;
DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINFORM),0,(DLGPROC)DlgProc);
return 0;
}
---------------------
[b]the problem:[/b]
BOOL CALLBACK DlgProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) {
switch(msg){
case WM_COMMAND:Handle_WM_COMMAND(hDlg,wParam,lParam);return TRUE;
case WM_INITDIALOG:Handle_WM_INITDIALOG(hDlg);return TRUE;
case WM_CLOSE:Handle_WM_CLOSE(hDlg);break;
case WM_NOTIFY:return Handle_WM_NOTIFY(hDlg,wParam,lParam);break;
case WM_KEYDOWN:MessageBox(0,"KeyDown","M",0);return(0);//[b]That never happens (???)[/b]
case WM_CONRESETBYSERVER:onServerDisconnect(hDlg,lParam);return TRUE;
}
return FALSE;
}

[/code]
«1

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : [code]
    : hi,
    : i want to handle the KEYDOWN event in a dialog i load through
    : DialogBox() functions. The problem is how ?,
    : -----------------------
    : [b]the program:[/b]
    : int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE phInstance,LPSTR cmd,int nShow){
    : hInst=hInstance;
    : DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINFORM),0,(DLGPROC)DlgProc);
    : return 0;
    : }
    : ---------------------
    : [b]the problem:[/b]
    : BOOL CALLBACK DlgProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) {
    : switch(msg){
    : case WM_COMMAND:Handle_WM_COMMAND(hDlg,wParam,lParam);return TRUE;
    : case WM_INITDIALOG:Handle_WM_INITDIALOG(hDlg);return TRUE;
    : case WM_CLOSE:Handle_WM_CLOSE(hDlg);break;
    : case WM_NOTIFY:return Handle_WM_NOTIFY(hDlg,wParam,lParam);break;
    : case WM_KEYDOWN:MessageBox(0,"KeyDown","M",0);return(0);//[b]That never happens (???)[/b]
    : case WM_CONRESETBYSERVER:onServerDisconnect(hDlg,lParam);return TRUE;
    : }
    : return FALSE;
    : }
    :
    : [/code]
    :
    [blue]It can be done by the custom dialog - read about it here:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/dialogboxes/dialogboxprogrammingconsiderations.asp[/blue]
  • xkgdiamxkgdiam Posts: 111Member
    [b][red]This message was edited by xkgdiam at 2003-11-19 16:10:53[/red][/b][hr]
    : : [code]
    : : hi,
    : : i want to handle the KEYDOWN event in a dialog i load through
    : : DialogBox() functions. The problem is how ?,
    : : -----------------------
    : : [b]the program:[/b]
    : : int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE phInstance,LPSTR cmd,int nShow){
    : : hInst=hInstance;
    : : DialogBox(hInstance,MAKEINTRESOURCE(IDD_MAINFORM),0,(DLGPROC)DlgProc);
    : : return 0;
    : : }
    : : ---------------------
    : : [b]the problem:[/b]
    : : BOOL CALLBACK DlgProc(HWND hDlg,UINT msg,WPARAM wParam,LPARAM lParam) {
    : : switch(msg){
    : : case WM_COMMAND:Handle_WM_COMMAND(hDlg,wParam,lParam);return TRUE;
    : : case WM_INITDIALOG:Handle_WM_INITDIALOG(hDlg);return TRUE;
    : : case WM_CLOSE:Handle_WM_CLOSE(hDlg);break;
    : : case WM_NOTIFY:return Handle_WM_NOTIFY(hDlg,wParam,lParam);break;
    : : case WM_KEYDOWN:MessageBox(0,"KeyDown","M",0);return(0);//[b]That never happens (???)[/b]
    : : case WM_CONRESETBYSERVER:onServerDisconnect(hDlg,lParam);return TRUE;
    : : }
    : : return FALSE;
    : : }
    : :
    : :
    i have allready download that file, i cant understand how to use it,
    and the only WM_COMMAND that refers to my edit box is wParam=0x03f3
    i think from 'winuser.h' is an initialization message.Can give you
    a littlte code to trap the keyboard ? Also there is no PeekMessage()
    and Dispatch() to the program,or i can load the dialog in some other way ?
    Here is my Dialog properties from the (rc) file, and the WM_COMMAND
    handler procedure :
    IDD_MAINFORM DIALOGEX 50,50,326,230
    STYLE WS_MINIMIZEBOX|WS_CAPTION|WS_SYSMENU
    CAPTION "Ws Client"
    FONT 9,"Courier New"
    BEGIN .........

    void Handle_WM_COMMAND(HWND hDlg,WPARAM wParam,LPARAM lParam){
    WORD wCmd=HIWORD(wParam);WORD wID=LOWORD(wParam);
    HWND hwndCtl=(HWND)lParam;
    // Menu Items
    if(wID==IDM_HELPABOUT){onClientAbout(hDlg);}
    if(wID==IDM_FILEEXIT){onClientClose(hDlg);}
    // Buttons
    if(wCmd==BN_CLICKED && wID==IDC_BT_CONNECT){onClientConnect(hDlg);}
    (.....)
    (......)
    if(lParam==(LPARAM)edUserIn && (WORD)wParam!=0x03f3){OnKeyUp(hDlg,wParam,lParam);}
    }
    edUserIn is the edit control i'm interesting in,
    but i want to understand the whole mechanism of this point on dialogs
    [/code]

    Thank for your time




  • xkgdiamxkgdiam Posts: 111Member
    [code]
    i found some work code,but after alot of experiment,
    cause no official documents were usefull.I'm very agry with this
    fact,the fact that the knowledge is hidding more and more every day
    hiding behide libraries and definition code that is difficult to
    see through it,and this is the main thought of the major s/w companies.
    Some years before knowing C/C++ you could cook with your machine,
    but a programmer that starts today learn this language through moderns
    tools and IDES would starve to death before he manage to draw a
    slice of bread in his screen in clear code or winApi.
    Althought there always exists programmers that look into their
    machines and become good and better every day,this will be more
    difficult from today to tomorrow and i don't like this because
    it is techichaly made to be like that and not because it is difficult
    by its nature.It's just bytes !
    (i cant speak the english very well,so i cant express myself quite
    well...)
    Anyway,
    here is the code i said so, (it works ok) but i like to know if
    thats the way to handle the messages for a dialog box
    int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE phInstance,LPSTR cmd,int nShow){
    hInst=hInstance;
    HWND h=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MAINFORM),0,(DLGPROC)DlgProc);
    MSG msg;
    WORD kstate;
    while(GetMessage(&msg,0,0,0))
    {
    kstate=GetKeyState(VK_CONTROL);
    if(msg.hwnd==edUserIn && msg.message==WM_KEYDOWN && msg.wParam==VK_RETURN && (kstate&0xff00)==0x0000)
    {MessageBox(0,"Pure RETURN","Return Key",0);continue;}
    TranslateMessage(&msg);DispatchMessage(&msg);
    }
    return msg.wParam;
    }
    [/code]

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Can you tell me what exactly the problem is? I feel that you doing it the wrong way. Why do you need to interact with keyboard in dialog? Windows is nicely designed - there is always something to hook on to...

    Also, your dialog template does not have a CLASS key, and if you read the link I posted - that is the requirement for custom dialog boxes.[/blue]
  • xkgdiamxkgdiam Posts: 111Member
    [b][red]This message was edited by xkgdiam at 2003-11-20 16:55:4[/red][/b][hr]
    [code]
    the problem was, that i have a dialog template from a rc file
    and i wanted to use it as a main window.

    the WndProc adress we pass in CreateWindow(...) api function
    accepts the WM_KEYDOWN messages from the system
    but the DlgProc callback we pass in DialogBox(...) or CreateDialog(...)
    doesnt.

    I cant understand how to solve it through the link you gave me,
    (i had allready download the doc),so ...all that expirements.

    This code i write works ok.
    What is the CLASS key, do you mean a class name for the dialog ?
    and if so should a dialog have something like this or sould be
    registered before open ???


    [/code]


  • weiccoweicco Posts: 950Member
    Does this example any good?

    [code]
    BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
    {
    switch (message)
    {
    case WM_INITDIALOG:
    return TRUE;

    case WM_KEYDOWN:
    MessageBox(hwnd, "Key was pressed!", NULL, 0);
    break;

    case WM_COMMAND:
    PostQuitMessage(0);
    break;

    default:
    break;
    }

    return FALSE;
    }

    int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
    {
    HWND hWnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
    ShowWindow(hWnd, SW_SHOW);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
    if (msg.message == WM_KEYDOWN)
    {
    SendMessage(hWnd, msg.message, msg.wParam, msg.lParam);
    }

    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }

    return 0;
    }
    [/code]
  • DarknezzDarknezz Posts: 245Member
    [b][red]This message was edited by Darknezz at 2003-11-21 2:16:2[/red][/b][hr]
    "Why do you need to interact with keyboard in dialog?"

    There could be many reasons to interact with the keyboard in a dialog. Even more so if you're using the dialog as your applications main window as in this case.

    And to xkgdiam, have you tried using GetAsyncKeyState() to check the state of cetain keys? You can call this from anywhere in your program and you don't even need to have a window procedure to do so (unlike GetKeyState()).

    You could do a "game style message loop" like so to keep checking the keys you wish to check..

    [code]
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    [green]//Create you window and do other setup here

    //"game style message loop"[/green]
    MSG msg;
    while(1)
    {
    [green]//test for a window message[/green]
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    [green]//we have amessage, is it a quit message?[/green]
    if(msg.message == WM_QUIT)
    break; [green]//break out of the message loop[/green]

    [green]//Translate and Dispatch the message[/green]
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }

    [green]//check keys using GetAsyncKeyState()[/green]
    if(GetAsyncKeyState(VK_UP) & 0x8000)
    MessageBox(hwnd, "Up Arrow Key is down!", NULL, MB_OK);
    }

    [green]//Return to windows[/green]
    return 0;
    }
    [/code]

    Lastly if you're using VC++ (i'm not sure about other compilers/IDEs) you can use the following macros to check if a key is up or down.

    [code]
    [green]//KEYDOWN returns true if the key is down or false if not[/green]
    #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

    [green]//KEYUP returns true if the key is up or false if not[/green]
    #define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
    [/code]

    *edit*
    Sorry, forgot to tell you that the Virtual Key Codes you pass to GetAsyncKeyState() are defined in winuser.h. They can also be found in the M$ Windows API reference.
    */edit*

    Hope this helps!
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : [b][red]This message was edited by Darknezz at 2003-11-21 2:16:2[/red][/b][hr]
    : "Why do you need to interact with keyboard in dialog?"
    :
    : There could be many reasons to interact with the keyboard in a dialog. Even more so if you're using the dialog as your applications main window as in this case.
    :

    [blue]The reason I asked is because usually something can be done without it easily. I had a dialog as a main window a lot and I never needed anything special.

    Also, how GetAsyncKeyState() can help? You need an event first and then ask if key state. Or you have to launch a thread and ask the key state in a loop - again - 'putting feet behind the ear'... maybe it is something simple, like catch a key when you inside an edit box - but there is subclassing for that. An no need to get a key state.[/blue]
  • DarknezzDarknezz Posts: 245Member
    : Also, how GetAsyncKeyState() can help? You need an event first and then ask if key state. Or you have to launch a thread and ask the key state in a loop - again - 'putting feet behind the ear'... maybe it is something simple, like catch a key when you inside an edit box - but there is subclassing for that. An no need to get a key state.[/blue]
    :

    "You need an event first and then ask if key state. Or you have to launch a thread and ask the key state in a loop."
    Wrong wrong wrong. Read my post again, carefully. I wrote a small code sample - "game style message loop" - which is a message loop that keeps on running even if the window recieves no messages. Look at it carefully, try it even :) You will find that it can check your key presses even if the window doesn't have focus and there is no way it can be recieving any messages.

    The reason why i suggested GetAsyncKeyState() is because this person obviously seems to have trouble using the method they were previously told about and i don't really know much about it so i helped out in the way i could, by suggesting GetAsyncKeyState(). By using this they can easily check the state of whatever keys they want and whenever they want.
  • xkgdiamxkgdiam Posts: 111Member
    [b][red]This message was edited by xkgdiam at 2003-11-21 14:12:8[/red][/b][hr]
    [code]
    i wanted different behavior between ENTER and ctr+ENTER and catch the
    ENTER key before adding new line in my EditBox, so i need no dispatching
    a such WM_KEYDOWN event, so i use GetKeyState(),i could use the
    GetAsyncState() also as 'Darknezz' said.

    The fact is that a DlgProc compete differs than a WndProc
    and if you want to continue your program running after a
    dialog box's load (i.e :to implement GetMessage() loops),
    you must use CreateDialog(..) and not DialogBox(...) command
    cause that blocks your programm until the dialog close.

    So,
    thank you all
    [/code]



«1
Sign In or Register to comment.