问题:如何获得数字上方的星星
我的小提琴目前没有检测到键盘上数字行上面的*,只有数字键盘上的*.
在我的键盘上,它是shift-3所以keyCode 51 + shift。不管我点击了什么来获得它,我如何测试它呢?
发布于 2012-03-24 17:57:38
keydown和keyup事件对于检测特定字符是不可靠的,因为这些事件的event.which属性是而不是字符代码。
必须使用keypress事件。当按下键时,此事件可能会多次触发。因此,当按下所需的键时,设置一个标志,并移除keyup上的标志。
演示:http://jsfiddle.net/xHnTD/
function something_to_do() {
// This function is fired when * is pressed.
$('<div>Pressed *!</div>').appendTo('body')
}
$('body').keypress(function(e) {
var $this = $(this);
if (e.which === 42) { // '*'.charCodeAt(0) === 42
if (!$this.data('rw_star_pressed')) {
$this.data('rw_star_pressed', true);
something_to_do();
}
}
}).keyup(function() {
$(this).removeData('rw_star_pressed');
});发布于 2012-03-23 14:53:21
我不太确定你贴的密码。使用此代码时,当我按*时,就会得到56
<script type="text/javascript">
document.addEventListener('keydown', function( event ) {
console.log(event.keyCode); });
</script>https://stackoverflow.com/questions/9841232
复制相似问题