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. How can get value of keyboard pressed key?

How can get value of keyboard pressed key?

Scheduled Pinned Locked Moved C / C++ / MFC
question
26 Posts 8 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.
  • L Offline
    L Offline
    Le rner
    wrote on last edited by
    #1

    Hi all, how can i get the value of pressed key of keyboard . how can check it is is "A to Z" or "0 tp 9" of special character. Thanks in advance.

    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

    C R B D 4 Replies Last reply
    0
    • L Le rner

      Hi all, how can i get the value of pressed key of keyboard . how can check it is is "A to Z" or "0 tp 9" of special character. Thanks in advance.

      IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Handle WM_CHAR[^] notification. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      R 1 Reply Last reply
      0
      • C CPallini

        Handle WM_CHAR[^] notification. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        CPallini wrote:

        Handle WM_CHAR[^] notification.

        What if the window had controls like editbox on it? (which is usually the case) :)

        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

        L 1 Reply Last reply
        0
        • L Le rner

          Hi all, how can i get the value of pressed key of keyboard . how can check it is is "A to Z" or "0 tp 9" of special character. Thanks in advance.

          IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          You may over-ride PreTranslateMessage()

          BOOL CTrialDlg::PreTranslateMessage(MSG* pMsg)
          {
          if(pMsg->message == WM_KEYDOWN){
          if(pMsg->wParam >= 0x41 && pMsg->wParam <= 0x5A)
          {
          //something between a and z was pressed
          }
          else if(pMsg->wParam >= 0x30 && pMsg->wParam <= 0x39)
          {
          //a number was pressed
          //check 0x60 through 0x69 to trap numbers
          //from the numpad keys as well
          }
          }
          return CDialog::PreTranslateMessage(pMsg);
          }

          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

          L 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            CPallini wrote:

            Handle WM_CHAR[^] notification.

            What if the window had controls like editbox on it? (which is usually the case) :)

            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

            L Offline
            L Offline
            Le rner
            wrote on last edited by
            #5

            Rajesh R Subramanian wrote:

            What if the window had controls like editbox on it? (which is usually the case)

            yes sir

            IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

            1 Reply Last reply
            0
            • R Rajesh R Subramanian

              You may over-ride PreTranslateMessage()

              BOOL CTrialDlg::PreTranslateMessage(MSG* pMsg)
              {
              if(pMsg->message == WM_KEYDOWN){
              if(pMsg->wParam >= 0x41 && pMsg->wParam <= 0x5A)
              {
              //something between a and z was pressed
              }
              else if(pMsg->wParam >= 0x30 && pMsg->wParam <= 0x39)
              {
              //a number was pressed
              //check 0x60 through 0x69 to trap numbers
              //from the numpad keys as well
              }
              }
              return CDialog::PreTranslateMessage(pMsg);
              }

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

              L Offline
              L Offline
              Le rner
              wrote on last edited by
              #6

              Rajesh R Subramanian wrote:

              BOOL CTrialDlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->lParam == WM_KEYDOWN){ if(pMsg->wParam >= 0x41 && pMsg->wParam <= 0x5A) { //something between a and z was pressed } else if(pMsg->wParam >= 0x30 && pMsg->wParam <= 0x39) { //a number was pressed //check 0x60 through 0x69 to trap numbers //from the numpad keys as well } } return CDialog::PreTranslateMessage(pMsg);}

              How can use it on purticular EditBox.

              IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

              S R 2 Replies Last reply
              0
              • L Le rner

                Rajesh R Subramanian wrote:

                BOOL CTrialDlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->lParam == WM_KEYDOWN){ if(pMsg->wParam >= 0x41 && pMsg->wParam <= 0x5A) { //something between a and z was pressed } else if(pMsg->wParam >= 0x30 && pMsg->wParam <= 0x39) { //a number was pressed //check 0x60 through 0x69 to trap numbers //from the numpad keys as well } } return CDialog::PreTranslateMessage(pMsg);}

                How can use it on purticular EditBox.

                IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

                S Offline
                S Offline
                sudhir_Kumar
                wrote on last edited by
                #7

                if(pMsg->hwnd == .m_hWnd) in your PreTranslateMessage

                -@SuDhIrKuMaR@-

                1 Reply Last reply
                0
                • L Le rner

                  Rajesh R Subramanian wrote:

                  BOOL CTrialDlg::PreTranslateMessage(MSG* pMsg) { if(pMsg->lParam == WM_KEYDOWN){ if(pMsg->wParam >= 0x41 && pMsg->wParam <= 0x5A) { //something between a and z was pressed } else if(pMsg->wParam >= 0x30 && pMsg->wParam <= 0x39) { //a number was pressed //check 0x60 through 0x69 to trap numbers //from the numpad keys as well } } return CDialog::PreTranslateMessage(pMsg);}

                  How can use it on purticular EditBox.

                  IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  change the initial if condition to something like this:

                  //IDC_EDIT1 is ID of the edit control of your interest
                  if(pMsg->message == WM_KEYDOWN && GetFocus()==GetDlgItem(IDC_EDIT1))
                  {
                  ...
                  }

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                  E 1 Reply Last reply
                  0
                  • L Le rner

                    Hi all, how can i get the value of pressed key of keyboard . how can check it is is "A to Z" or "0 tp 9" of special character. Thanks in advance.

                    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

                    B Offline
                    B Offline
                    buntyrolln
                    wrote on last edited by
                    #9

                    Use Richedit instead of smple Editbox. There you can add function on message EN_MSGFILTER. This permits filtering of mouse and keyboard message in the control. TODO: The control will not send this notification unless you override the CDialog::OnInitDialog() function to send the EM_SETEVENTMASK message to the control with either the ENM_KEYEVENTS or ENM_MOUSEEVENTS flag ORed into the lParam mask. inside OnInitDialog mask the key events m_rich.SetEventMask(m_rich.GetEventMask()|ENM_KEYEVENTS); `` void CExampleDlg::OnMsgfilter(NMHDR* pNMHDR, LRESULT* pResult) { MSGFILTER *pMsgFilter = reinterpret_cast(pNMHDR); //TODO: Add your control notification handler code here CString buf; if(pMsgFilter->msg==WM_CHAR ) { buf+=pMsgFilter->wParam; } *pResult = 0; }

                    buntyrolln

                    1 Reply Last reply
                    0
                    • L Le rner

                      Hi all, how can i get the value of pressed key of keyboard . how can check it is is "A to Z" or "0 tp 9" of special character. Thanks in advance.

                      IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      If you derive a class from CEdit, you can override OnChar().

                      "Love people and use things, not love things and use people." - Unknown

                      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                      1 Reply Last reply
                      0
                      • R Rajesh R Subramanian

                        change the initial if condition to something like this:

                        //IDC_EDIT1 is ID of the edit control of your interest
                        if(pMsg->message == WM_KEYDOWN && GetFocus()==GetDlgItem(IDC_EDIT1))
                        {
                        ...
                        }

                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                        E Offline
                        E Offline
                        Eytukan
                        wrote on last edited by
                        #11

                        if(pMsg->hwnd == GetDlgItem(IDC_EDIT1)->m_hWnd) ;P ptimization!


                        OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                        R 1 Reply Last reply
                        0
                        • E Eytukan

                          if(pMsg->hwnd == GetDlgItem(IDC_EDIT1)->m_hWnd) ;P ptimization!


                          OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                          R Offline
                          R Offline
                          Rajesh R Subramanian
                          wrote on last edited by
                          #12

                          Do you mean to say that it isn't optimized? Have you examined the generated assembly? It would be essentially the same. Or, is that you're just kidding? :)

                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                          E 1 Reply Last reply
                          0
                          • R Rajesh R Subramanian

                            Do you mean to say that it isn't optimized? Have you examined the generated assembly? It would be essentially the same. Or, is that you're just kidding? :)

                            Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                            E Offline
                            E Offline
                            Eytukan
                            wrote on last edited by
                            #13

                            I was kidding but it has to be true too. See, How come a getfocus() "call" be equivalent to a structure that's already coming in? Why do want to make an extra call to get the hwnd? Moreover getFocus() deals with CWnd* And how would the assemblies just match in parallel?


                            OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                            R 1 Reply Last reply
                            0
                            • E Eytukan

                              I was kidding but it has to be true too. See, How come a getfocus() "call" be equivalent to a structure that's already coming in? Why do want to make an extra call to get the hwnd? Moreover getFocus() deals with CWnd* And how would the assemblies just match in parallel?


                              OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                              R Offline
                              R Offline
                              Rajesh R Subramanian
                              wrote on last edited by
                              #14

                              It is not the assemblies being a "match". The essence of my statement is that the compiler will optimize it to a very reasonable extent, thereby making it the same. Simply because it "knows" what I'm trying to do. Everything becomes assembler instructions there. There's no API call or CWnd pointer to deal with. Do you agree that the following two snippets are essentially equally optimized? (not by way of code, but by way of the assembly the compiler generates)

                              while(condition)
                              {
                              CStringArray str;
                              __int64 nBigAss[10];
                              //use str and nBigAss
                              }

                              CStringArray str;
                              __int64 nBigAss[10];
                              while(condition)
                              {
                              //use str and nBigAss
                              }

                              One may argue "There is an allocation every time the loop runs". Well, I don't think so. (No, I ain't going to kill time generating and rearranging assembly with the PreTranslateMessage() stuff, but I can almost say it for sure) :)

                              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                              E 1 Reply Last reply
                              0
                              • R Rajesh R Subramanian

                                It is not the assemblies being a "match". The essence of my statement is that the compiler will optimize it to a very reasonable extent, thereby making it the same. Simply because it "knows" what I'm trying to do. Everything becomes assembler instructions there. There's no API call or CWnd pointer to deal with. Do you agree that the following two snippets are essentially equally optimized? (not by way of code, but by way of the assembly the compiler generates)

                                while(condition)
                                {
                                CStringArray str;
                                __int64 nBigAss[10];
                                //use str and nBigAss
                                }

                                CStringArray str;
                                __int64 nBigAss[10];
                                while(condition)
                                {
                                //use str and nBigAss
                                }

                                One may argue "There is an allocation every time the loop runs". Well, I don't think so. (No, I ain't going to kill time generating and rearranging assembly with the PreTranslateMessage() stuff, but I can almost say it for sure) :)

                                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                E Offline
                                E Offline
                                Eytukan
                                wrote on last edited by
                                #15

                                Yes modern compiler does these. But it will never optimize the way you say about "getfocus()". When you ask a compiler to make a call, it has to call. It doesn't know what you are going to do with the CWnd*. Not that intelligent dude ;p. So I still can't agree. :cool:


                                OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                                R 1 Reply Last reply
                                0
                                • E Eytukan

                                  Yes modern compiler does these. But it will never optimize the way you say about "getfocus()". When you ask a compiler to make a call, it has to call. It doesn't know what you are going to do with the CWnd*. Not that intelligent dude ;p. So I still can't agree. :cool:


                                  OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                                  R Offline
                                  R Offline
                                  Rajesh R Subramanian
                                  wrote on last edited by
                                  #16

                                  Actually, you were right the first time. I was just farting making up things, only because I said it. Probably you knew it too. And yes, I could have avoided a function call. You caught me there. :sigh: But ya know? I typed an answer on reading the query immediately. No opening of visual studio, no cheating. It couldn't be the most optimized answer and optimization can be left to the OP, right? You can understand, can't you? :-D

                                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                  E N 3 Replies Last reply
                                  0
                                  • R Rajesh R Subramanian

                                    Actually, you were right the first time. I was just farting making up things, only because I said it. Probably you knew it too. And yes, I could have avoided a function call. You caught me there. :sigh: But ya know? I typed an answer on reading the query immediately. No opening of visual studio, no cheating. It couldn't be the most optimized answer and optimization can be left to the OP, right? You can understand, can't you? :-D

                                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                    E Offline
                                    E Offline
                                    Eytukan
                                    wrote on last edited by
                                    #17

                                    Rajesh R Subramanian wrote:

                                    Actually, you were right the first time. I was just farting making up things, only because I said it. Probably you knew it too. And yes, I could have avoided a function call. You caught me there

                                    I was actually kidding + tried to provoke you :D

                                    Rajesh R Subramanian wrote:

                                    But ya know? I typed an answer on reading the query immediately. No opening of visual studio, no cheating. It couldn't be the most optimized answer and optimization can be left to the OP, right? You can understand, can't you?

                                    lol :) . Now I'm free to agre that you know more MFC than me. That's true.


                                    OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                                    1 Reply Last reply
                                    0
                                    • R Rajesh R Subramanian

                                      Actually, you were right the first time. I was just farting making up things, only because I said it. Probably you knew it too. And yes, I could have avoided a function call. You caught me there. :sigh: But ya know? I typed an answer on reading the query immediately. No opening of visual studio, no cheating. It couldn't be the most optimized answer and optimization can be left to the OP, right? You can understand, can't you? :-D

                                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                      N Offline
                                      N Offline
                                      Nish Nishant
                                      wrote on last edited by
                                      #18

                                      Rajesh R Subramanian wrote:

                                      It couldn't be the most optimized answer and optimization can be left to the OP, right?

                                      I think it's not just about optimization here. GetFocus and GetDlgItem can potentially return temporary CWnd objects - so comparing them won't always be the right thing to do. So Vunic's code seems to be the right way here.

                                      Regards, Nish


                                      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                      My latest book : C++/CLI in Action / Amazon.com link

                                      R 2 Replies Last reply
                                      0
                                      • R Rajesh R Subramanian

                                        Actually, you were right the first time. I was just farting making up things, only because I said it. Probably you knew it too. And yes, I could have avoided a function call. You caught me there. :sigh: But ya know? I typed an answer on reading the query immediately. No opening of visual studio, no cheating. It couldn't be the most optimized answer and optimization can be left to the OP, right? You can understand, can't you? :-D

                                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                        E Offline
                                        E Offline
                                        Eytukan
                                        wrote on last edited by
                                        #19

                                        We have made enough noise to wake up the gaint! :omg: See who has replied :D


                                        OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus Best wishes to Rexx[^]

                                        R 1 Reply Last reply
                                        0
                                        • N Nish Nishant

                                          Rajesh R Subramanian wrote:

                                          It couldn't be the most optimized answer and optimization can be left to the OP, right?

                                          I think it's not just about optimization here. GetFocus and GetDlgItem can potentially return temporary CWnd objects - so comparing them won't always be the right thing to do. So Vunic's code seems to be the right way here.

                                          Regards, Nish


                                          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                                          My latest book : C++/CLI in Action / Amazon.com link

                                          R Offline
                                          R Offline
                                          Rajesh R Subramanian
                                          wrote on last edited by
                                          #20

                                          Indeed, look at my previous reply (and vote) to him. :)

                                          Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                                          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