Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 880231 - overriding keyboard behaviour

880231 - overriding keyboard behaviour

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ilostmyid2
    wrote on last edited by
    #1

    hi as we know, in a dialog box Tab and Shift+Tab change focus between ctrls, Tab to the next one and Shift+Tab to the previous one. when handling the keyboard event generated, we see that both generates the same event with the same wParam (0x09) and lParam (0x000d0001). so how a dialog figures out which one is hit? apparently by using the function GetKeyState at the moment of handling the message. alright? now, in a system with no tab key i want to use Up and Down keys in the place of them. at this moment the problem gets produced. in the PreTranslateMessage of my application, i just change wParam from VK_DOWN into VK_TAB. the PreTranslateMessage of the base class (CWinApp) thinks Tab is hit instead of Down arrow. this is the behavior i intend and it's ok. but what can i do when the user hits Up arrow and expect the previous ctrl gets focused? do i need to use SetKeyboardState and this is the only way to simulate hitting Shift key? what can i do? thx

    J S 2 Replies Last reply
    0
    • I ilostmyid2

      hi as we know, in a dialog box Tab and Shift+Tab change focus between ctrls, Tab to the next one and Shift+Tab to the previous one. when handling the keyboard event generated, we see that both generates the same event with the same wParam (0x09) and lParam (0x000d0001). so how a dialog figures out which one is hit? apparently by using the function GetKeyState at the moment of handling the message. alright? now, in a system with no tab key i want to use Up and Down keys in the place of them. at this moment the problem gets produced. in the PreTranslateMessage of my application, i just change wParam from VK_DOWN into VK_TAB. the PreTranslateMessage of the base class (CWinApp) thinks Tab is hit instead of Down arrow. this is the behavior i intend and it's ok. but what can i do when the user hits Up arrow and expect the previous ctrl gets focused? do i need to use SetKeyboardState and this is the only way to simulate hitting Shift key? what can i do? thx

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      Maybe this[^] article will help, with a couple of possible alternatives.

      1 Reply Last reply
      0
      • I ilostmyid2

        hi as we know, in a dialog box Tab and Shift+Tab change focus between ctrls, Tab to the next one and Shift+Tab to the previous one. when handling the keyboard event generated, we see that both generates the same event with the same wParam (0x09) and lParam (0x000d0001). so how a dialog figures out which one is hit? apparently by using the function GetKeyState at the moment of handling the message. alright? now, in a system with no tab key i want to use Up and Down keys in the place of them. at this moment the problem gets produced. in the PreTranslateMessage of my application, i just change wParam from VK_DOWN into VK_TAB. the PreTranslateMessage of the base class (CWinApp) thinks Tab is hit instead of Down arrow. this is the behavior i intend and it's ok. but what can i do when the user hits Up arrow and expect the previous ctrl gets focused? do i need to use SetKeyboardState and this is the only way to simulate hitting Shift key? what can i do? thx

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        The other way to do it would be to replicate Windows tab-handling for VK_UP and VK_DOWN in the PreTranslateMessage method...

        BOOL CtestmfcDlg::PreTranslateMessage(MSG* pMsg)
        {
        if (pMsg->message == WM_KEYDOWN)
        {
        if (pMsg->wParam == VK_UP)
        {
        CWnd* prev = GetFocus()->GetNextWindow(GW_HWNDPREV);
        if (!prev) prev = GetFocus()->GetNextWindow(GW_HWNDLAST);
        if (prev) prev->SetFocus();
        return TRUE;
        }
        else if (pMsg->wParam == VK_DOWN)
        {
        CWnd* next = GetFocus()->GetNextWindow(GW_HWNDNEXT);
        if (!next) next = GetFocus()->GetNextWindow(GW_HWNDFIRST);
        if (next) next->SetFocus();
        return TRUE;
        }
        }

        return CDialog::PreTranslateMessage(pMsg);
        }

        (tested with VS2010 Beta 1 - woo-hoo!)

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        I 2 Replies Last reply
        0
        • S Stuart Dootson

          The other way to do it would be to replicate Windows tab-handling for VK_UP and VK_DOWN in the PreTranslateMessage method...

          BOOL CtestmfcDlg::PreTranslateMessage(MSG* pMsg)
          {
          if (pMsg->message == WM_KEYDOWN)
          {
          if (pMsg->wParam == VK_UP)
          {
          CWnd* prev = GetFocus()->GetNextWindow(GW_HWNDPREV);
          if (!prev) prev = GetFocus()->GetNextWindow(GW_HWNDLAST);
          if (prev) prev->SetFocus();
          return TRUE;
          }
          else if (pMsg->wParam == VK_DOWN)
          {
          CWnd* next = GetFocus()->GetNextWindow(GW_HWNDNEXT);
          if (!next) next = GetFocus()->GetNextWindow(GW_HWNDFIRST);
          if (next) next->SetFocus();
          return TRUE;
          }
          }

          return CDialog::PreTranslateMessage(pMsg);
          }

          (tested with VS2010 Beta 1 - woo-hoo!)

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          I Offline
          I Offline
          ilostmyid2
          wrote on last edited by
          #4

          this is a good method. indeed doing what the dialog itself does in handling these keys again in our code for another keys. but when there're many dialogs in the program and i'm not going to derive them from a common base and put this code in its PreTranslateMessage method makes it a bit not a straightforward method. the code i've to write is what the others also r working on. all keyboard handlings must occur in the application's PreTranslateMessage. i know that it's not a logical limitation, but this is what i can do.

          1 Reply Last reply
          0
          • S Stuart Dootson

            The other way to do it would be to replicate Windows tab-handling for VK_UP and VK_DOWN in the PreTranslateMessage method...

            BOOL CtestmfcDlg::PreTranslateMessage(MSG* pMsg)
            {
            if (pMsg->message == WM_KEYDOWN)
            {
            if (pMsg->wParam == VK_UP)
            {
            CWnd* prev = GetFocus()->GetNextWindow(GW_HWNDPREV);
            if (!prev) prev = GetFocus()->GetNextWindow(GW_HWNDLAST);
            if (prev) prev->SetFocus();
            return TRUE;
            }
            else if (pMsg->wParam == VK_DOWN)
            {
            CWnd* next = GetFocus()->GetNextWindow(GW_HWNDNEXT);
            if (!next) next = GetFocus()->GetNextWindow(GW_HWNDFIRST);
            if (next) next->SetFocus();
            return TRUE;
            }
            }

            return CDialog::PreTranslateMessage(pMsg);
            }

            (tested with VS2010 Beta 1 - woo-hoo!)

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            I Offline
            I Offline
            ilostmyid2
            wrote on last edited by
            #5

            please notice the following code i put in the application's PreTranslateMessage: ... case VK_DOWN: pMsg->wParam=VK_TAB; BYTE byKeybState[256]; GetKeyboardState(byKeybState); byKeybState[VK_SHIFT] = !(BOOL)GetKeyState(VK_SHIFT); // or TRUE SetKeyboardState(byKeybState); break; ... it must work. right? but in CE it seems that GetKeyboardState and SetKeyboardState functions r not available. what can i do?

            S 1 Reply Last reply
            0
            • I ilostmyid2

              please notice the following code i put in the application's PreTranslateMessage: ... case VK_DOWN: pMsg->wParam=VK_TAB; BYTE byKeybState[256]; GetKeyboardState(byKeybState); byKeybState[VK_SHIFT] = !(BOOL)GetKeyState(VK_SHIFT); // or TRUE SetKeyboardState(byKeybState); break; ... it must work. right? but in CE it seems that GetKeyboardState and SetKeyboardState functions r not available. what can i do?

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              I'd use GetAsncKeyState[^] (which is in CE) and the SendInput technique documented in the other answer. The following pseudo-code shows the approach I'd use:

              if (GetAsyncKeyState shows Shift is pressed)
              {
              use SendInput to send { Tab key press, Tab key release };
              }
              else
              {
              use SendInput to send { Shift key press, Tab key press, Tab key release, Shift key release };
              }

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              I 1 Reply Last reply
              0
              • S Stuart Dootson

                I'd use GetAsncKeyState[^] (which is in CE) and the SendInput technique documented in the other answer. The following pseudo-code shows the approach I'd use:

                if (GetAsyncKeyState shows Shift is pressed)
                {
                use SendInput to send { Tab key press, Tab key release };
                }
                else
                {
                use SendInput to send { Shift key press, Tab key press, Tab key release, Shift key release };
                }

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                I Offline
                I Offline
                ilostmyid2
                wrote on last edited by
                #7

                thank u :) i think this is the solution i was looking for. i'm going to implement it.

                I 1 Reply Last reply
                0
                • I ilostmyid2

                  thank u :) i think this is the solution i was looking for. i'm going to implement it.

                  I Offline
                  I Offline
                  ilostmyid2
                  wrote on last edited by
                  #8

                  yeah, that's it! thx :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups