检查 "change" 事件是否来自键盘

check if "change" event is from keyboard

我有 on change 触发器获取事件 :

  $(document).on('change', '.class', function (eve) {

如何通过事件变量知道更改是来自鼠标还是来自键盘?

要确保事件来自键盘,请使用 keydown。要确保它来自鼠标,请使用 mousekeydown。因此,您需要为每个设备注册 2 个不同的事件:

$(document).on('keydown', '.class', function (e) {
   // From keyboard
}
$(document).on('mousekeydown', '.class', function (e) {
   // From mouse
}

不,这是不可能的,因为 change 事件可能仅在相关元素失去焦点时触发,在使用多种不同的输入法(包括鼠标和键盘)进行多次更改后。

使用此 input 元素进行测试:使用键盘输入一些文本,使用上下文菜单(右键单击)粘贴一些文本,并可能通过使用鼠标将某些字符拖动到不同位置来移动它们。然后按下按钮。只有现在你得到 change 事件:

$(document).on('change', '.class', function (eve) {
    console.log('change event occurred');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="class">
<button>Button</button>