简体   繁体   中英

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;
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM