'C# use SendMessage send data to a html and trigger keyup event

I am trying to send data to a html page by using SendMessage . It worked well except the enter key!Here is the code in the html:

<script src="Scripts/jquery-1.8.2.min.js"></script>
    <script>
        var code = "";
        $(function () {
            addKeyPressListener();
        });
        function  documentKeyPress(t) {
            console.log(t);
            console.log(t.which);
            if (13 === t.which) {
                return  handleTraceCode();
            }
             code += t.key;

        };
        function  addKeyPressListener() {
            document.addEventListener("keyup", this.documentKeyPress, !0);
        };
        function handleTraceCode() {
         
            console.log("scanbarcode:", code);
            code = "";
        };
    </script>

Here is the code.It worked well except the enter key!

public bool SendText(string text)
{
    IntPtr hwnd = GetForegroundWindow();
    if (String.IsNullOrEmpty(text))
    {
        return false;
    }
    GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
    if (guiInfo != null)
    {
        for (int i = 0; i < text.Length; i++)
        {
            SendMessage(guiInfo.Value.hwndFocus, 0x0101, (IntPtr)(int)text[i], new IntPtr(0)); //can trigger keyup event
            Thread.Sleep(5);
        }
        Thread.Sleep(5);
        SendMessage(guiInfo.Value.hwndFocus, 0x0101, (IntPtr)13, new IntPtr(0));//can not trigger keyup event
        return true;
    }
   return false;
}


Solution 1:[1]

SendMessage(guiInfo.Value.hwndFocus, 0x0100, (IntPtr)0x0D, IntPtr.Zero); add this code? SendMessage(guiInfo.Value.hwndFocus, 0x0101, (IntPtr)0x0D, IntPtr.Zero);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chcc8899