<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>
下面是SendMessage 相关代码
public bool SendText(string text, out string msg)
{
msg = "OK";
IntPtr hwnd = GetForegroundWindow();
if (String.IsNullOrEmpty(text))
{
msg = "数据为空";
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)); //可以正常触发
Thread.Sleep(5);
}
Thread.Sleep(5);
SendMessage(guiInfo.Value.hwndFocus, 0x0101, (IntPtr)13, new IntPtr(0));//不能触发
return true;
}
else
{
msg = "未找到可输出的界面";
return false;
}
}
string msg = "";
SendText("123", out msg);
System.Windows.Forms.SendKeys.Send("{ENTER}"); 试过了可以,但是我想知道为什么SendMessage不行呢
前者是属于 SendKeys类, 它的功能是利用击键和击键组合发送到活动应用程序。
SendMessage返回消息被处理后的返回值,不过我了解的是message,你的代码里有下面这些嘛?
using System;
using System.Drawing;
using System.Windows.Forms;