我有一个与一个键交互的代码,如果你按下它就会得到一个结果,我想知道它是否可以同时与两个键交互,例如按下Ctrl和A并得到一个结果。< / p>
感谢。
public void addText(String text, boolean timestamp) {
long timeMS = System.currentTimeMillis();
Date instant = new Date(timeMS);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String time = sdf.format(instant);
boolean shouldScroll = false;
try {
HTMLDocument doc = (HTMLDocument) getChat().getDocument();
HTMLEditorKit kit = (HTMLEditorKit) getChat().getEditorKit();
JScrollBar vsb = getChatScroller().getVerticalScrollBar();
BoundedRangeModel model = vsb.getModel();
if (model.getExtent() + model.getValue() == model.getMaximum())
shouldScroll = true;
kit.insertHTML(doc, doc.getLength(), timestamp ? time + ": " + text : text, 0, 0, null);
kit.insertHTML(doc, doc.getLength(), "<IMG SRC=file://res/picture.png>", 0, 0, HTML.Tag.IMG);
if (shouldScroll)
getChat().setCaretPosition(doc.getLength());
} catch (IOException | BadLocationException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
如果已按下event.ctrlKey
,您可以查看:
document.addEventListener('keydown', function(event) {
console.log(event.ctrlKey)
}, false)
答案 1 :(得分:1)
document.addEventListener('keydown', function(event) {
if(event.ctrlKey && (event.keyCode === 76)) {
document.getElementById('key').innerHTML = 'Get Money';
}
});
document.addEventListener('keyup', function() {
document.getElementById('key').innerHTML = 'Give It Back';
});
<div id="key"></div>
答案 2 :(得分:0)
在您的事件处理程序中检查Control是否已关闭:http://www.w3schools.com/jsref/event_ctrlkey.asp