简体   繁体   English

将键盘输入保存到变量中

[英]Saving keyboard input into a variable

I wrote a Firefox extension that gets input from a Barcode Tag Scanner, calls a page and writes it into a field. 我编写了一个Firefox扩展程序,该扩展程序从条形码标签扫描程序获取输入,调用页面并将其写入字段。 The scanner is programmed to send a keystroke after reading a tag. 扫描仪被编程为在读取标签后发送击键。 I added an event on the window object that is listening for CTRL K . 我在正在监听CTRL K的窗口对象上添加了一个事件。 My problem is the focus on the textbox, when it's lost it's not working. 我的问题是将重点放在文本框上,当它丢失时,它将无法正常工作。 My question is: is it possibile to get the value of the scanned tag without using the textbox element? 我的问题是:是否可以在不使用textbox元素的情况下获取已扫描标签的值? Scanner input comes in via keyboard interface. 扫描仪输入通过键盘界面输入。

BrowserOverlay.xul : BrowserOverlay.xul

<window id="main-window">
<script type="application/x-javascript">

window.addEventListener("keydown", barcodeInit, false);

function sel(){
var textbox = document.getElementById("editIndex");
textbox.focus();
}
</script>
<hbox>
  <spacer flex="1"/>
<textbox id="editIndex" value="" size="1" maxlength="25" oncommand="sel()" />
<statusbar id="status-bar" class="chromeclass-status"> 
<statusbarpanel label="Barcode AddOn" class="statusbarpanel-iconic"
      onclick="openPreferences('barcodePane');" align="left" 
          src="chrome://topas/content/images/icon18.png" />
</statusbar>

</hbox>

When I get the textbox content I'm calling the openURI method to open the page in the actual window and post data to it . 当我得到文本框内容时,我将调用openURI方法在实际窗口中打开页面并将数据发布到该窗口。

BrowserOverlay.js : BrowserOverlay.js

if (event.ctrlKey && pressedKey == prefKey ){
  if(typeof editIndex != "undefined"){
    var dataString = "barcodeValue=" + editIndex;
    immidiate = true;
    document.getElementById("editIndex").value = "";
    window.loadURI(stringURL, null, dataString, false);
  }

You can receive keys pressed by listening for the keypress events on the window object - you will get them as long as the browser window is focused, no matter which element of the window is active. 您可以通过侦听window对象上的keypress事件来接收按下的keypress -只要浏览器窗口处于焦点状态,无论该窗口的哪个元素处于活动状态,您都将获得它们。 Along these lines: 遵循以下原则:

var data = "";
window.addEventListener("keypress", processKeyPress, false);
window.addEventListener("keydown", processKeyDown, false);

function processKeyPress(event)
{
  data += String.fromCharCode(event.charCode);
}

function processKeyDown(event)
{
  if (event.ctrlKey && event.keyCode == prefKey)
  {
    var dataString = "barcodeValue=" + data;
    data = "";
    ...
  }
}

Documentation: event.charCode 文档: event.charCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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