none
problem with using keybd_event to simulate keyboard RRS feed

  • Question

  • I encounter a problem with using keybd_event  to simulate keyboard.  Basically I want to build a soft keyboard and input Chinese in an editbox.  I create an editbox and a window that has a group of buttons to simulate the keyboard buttons. When a button is pressed, I use keybd_event to simulate a keypress from hard keyboard.  I have no problem in inputting Chinese via Microsoft Pinyin on windows 7 and some versions of win10, but when I recently migrate the application to win10 1809, it fails to work any more. for example, I want to input the charater "方“ by input continuously the 4 keys "f" "a" "n" "g",  it will just input "fang" instead of "方“ for me.   Can anybody help with this?  Has newest version of Win10 changed something related?


    Wednesday, October 9, 2019 7:13 AM

Answers

  • Hi,

    I think the issue is that, the button is also a CWnd class(if you use MFC), which is a kind of window. When you click on it, the focus of the window will change. Although you add the code that focuses the window focus on the edit box in the click event of the button, the window focus remains the same for a moment. It will change, so the content of the input method will be cleared.

    The same issue exists in an Chinese case of MSDN, reproduce the issue in win10 1903. It provides a workaround.

    Keybd_event is a global analog button, only for the foreground window (that is, the current active window), but if the simulated button is also a global hotkey message for a window, then the window can also receive .SendMessage, PostMessage is For the specified handle window, if you are typing the specified EditBox.

    About the win10 version, I don't find much information. Maybe this can be helpful. I'm not sure.

    Best regards,

    Jeffrey


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Jeffrey Shao Wednesday, October 9, 2019 9:31 AM
    • Marked as answer by NKJayFang Monday, October 14, 2019 3:29 AM
    Wednesday, October 9, 2019 9:31 AM

All replies

  • Hi,

    I think the issue is that, the button is also a CWnd class(if you use MFC), which is a kind of window. When you click on it, the focus of the window will change. Although you add the code that focuses the window focus on the edit box in the click event of the button, the window focus remains the same for a moment. It will change, so the content of the input method will be cleared.

    The same issue exists in an Chinese case of MSDN, reproduce the issue in win10 1903. It provides a workaround.

    Keybd_event is a global analog button, only for the foreground window (that is, the current active window), but if the simulated button is also a global hotkey message for a window, then the window can also receive .SendMessage, PostMessage is For the specified handle window, if you are typing the specified EditBox.

    About the win10 version, I don't find much information. Maybe this can be helpful. I'm not sure.

    Best regards,

    Jeffrey


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Jeffrey Shao Wednesday, October 9, 2019 9:31 AM
    • Marked as answer by NKJayFang Monday, October 14, 2019 3:29 AM
    Wednesday, October 9, 2019 9:31 AM
  • I apologize if the following is not relevant to your situation.

    It is often a beginner mistake to believe that sending keystrokes in that manner is a simple and effective solution. Some experienced developers are successful with doing it but it is more complicated than most beginners realize. Most experienced developers send keyboard messages instead of keystrokes.

    Perhaps that is not the best solution for what you are doing but it is usually easier overall. Since you have been successful sending keystrokes then I understand it might be best but have you considered sending keyboard messages to the relevant controls?



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, October 9, 2019 8:29 PM
  • It is surely relevant. 

    I am a  beginner in windows programming, but how can I send keyboard messages to an Edit control? 

    LRESULT SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)

    I guess can put the key information in the wParam or the  IParam, but how will the Edit control get the message and do the corresponding keyboard input?

    I noticed that an Edit control is predefined window control and I am not very sure how to obtain the message.

    Thursday, October 10, 2019 7:19 AM
  • Thanks for your analysis. It does make sense in terms of the focus.  The solution may lie in how to avoid using Keybd_event by using SendMessage, but is there anyway to send message to a predefined control such as Edit ?
    Thursday, October 10, 2019 7:22 AM
  • See my Clicking a Button in Another Application. You do not say what language you are using. My article uses C#; if you are using some other language then I hope you can understand anyway. The article uses a button control but the concepts apply to edit controls too. It describes use of Spy++ and that should answer many of your questions. So if you need to you can use Spy++ with something like Notepad to get samples of messages you need to send.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, October 10, 2019 3:27 PM
  • Thanks a lot but it does not seem to answer my question.  I know how to send via SendMessage, but do not know how to get this message on the edit box side.  I am using c language in a windows console application.  There is only one process involved.
    Friday, October 11, 2019 3:29 AM
  • Thanks a lot but it does not seem to answer my question.  I know how to send via SendMessage, but do not know how to get this message on the edit box side.  I am using c language in a windows console application.  There is only one process involved.

    You receive the key input the same as if someone typed it.

    There is much that I do not understand. Are you saying you have a window with an edit control in it that is created in a console application?



    Sam Hobbs
    SimpleSamples.Info

    Friday, October 11, 2019 6:33 AM
  • it is a good point to not use a button.  Instead if I use a bitmap picture and on WM_LBUTTONDOWN, the initial problem is solved on win10 1803 even still using keybd_event .  But there are still problems on win10 1903.  What the hell is the difference between 1803 and 1903 in this regard?

    I have also tried SendInput and SendMessage, pretty much the same though.

    Friday, October 11, 2019 6:53 AM
  • You receive the key input the same as if someone typed it.

    There is much that I do not understand. Are you saying you have a window with an edit control in it that is created in a console application?

    yes.  I have a window console application in which there is an edit control where I try to input chinese via a soft keyboard.  The soft keyboard must also be created in this same application.  There is no problem if I use a separate process to run Microsoft OSK(on screen keyboard).  In lower version of win10, there was no problem using keybd_event to simulate the hard keyboard. The problem arises in newest versions of win10 such as 1903.  

    Saturday, October 12, 2019 12:39 AM
  • What the hell is the difference between 1803 and 1903 in this regard?

    1903 has quite a lot of subtle changes. Some of them are related to changes in underlying console functionality.

    As them Chinese say... everything passes, only the change remains ?

    -- pa

    Saturday, October 12, 2019 4:42 AM
  • 1903 has quite a lot of subtle changes. Some of them are related to changes in underlying console functionality.

    It seems so. I am trying to avoid predefined consoles such as a button and may solve the problem. Thanks!

    Saturday, October 12, 2019 7:04 AM
  • Jeffrey,  it is a good suggestion to not use the Button control but to use picture bitmap for the keys.  Calculate the clicked coordinate and respond the WM_LBUTTONUP message.  (sometimes the problem still exists if you use WM_LBUTTONDOWN).

    Monday, October 14, 2019 3:29 AM