Execute Key Press Event

浪尽此生 提交于 2021-02-08 10:14:04

问题


I have been trying to develop a POS solution and opening the Till Drawer has been the hardest part so far.

Currently I am trying to do this via a combination of keys. Ie CTRL+SHIFT+p. What I need to do is execute those keys when a certain function is called. I have built the system and also have the function ready and waiting.

I am using Auto hot Key to map these keys to an exe which opens the drawer. If I press the keys on my keyboard this works perfectly fine. But rather than having the user press these keys, I would like to execute them automatically. I have searched quite a bit and many solutions are listening for if a key is pressed and then reacting accordingly.

The current code I have found that has gotten me slightly forward is the following (this simulates the press of CTRL+SHITF+p from what I understand):

e = jQuery.Event("keypress")
    e.which = 80; //choose the one you want
    e.ctrlKey = true;
    e.shiftKey = true;
    $("#pos-paid").keypress(function(){alert('keypress triggered')}).trigger(e)

This does alert me that the key has been pressed. Jquery is definitely not my strong point so forgive me if the function is nonsense.

I am using Chrome and understand there are security issues to consider. Some people say this is impossible others say it is possible.

Can someone tell me if this is achievable and if so can someone point my in the right direction of how to achieve this or some similar questions/docs for this?

Notes I have currently tried creating an extension, had issues accessing local files.

I have tried creating an extension that communicates with an app, but also ran into issues.

I want to avoid any solutions that require networking.

I own the client machines that will access the site and therefore can prepare this and add any software necessary to achieve this.

Here are my other two questions for this issue.

Communicate with Cash Drawer from Website

Access Local Files using a Google Chrome Extension


回答1:


There should be no problem if you are executing an KeyboardEvent from a content script. The jQuery code will not work.

Here is a function that will trigger a CTRL + SHIFT + p 'keydown' event. Also Chrome is buggy on this and needs a pollyfill to make it work:

var event_object = document.getElementById('pos-paid');

function emitKeyEvent() {

    //    This function triggers the event
    var keyEvent = new KeyboardEvent("keypress", {key : "p", char : "p", ctrlKey: true, shiftKey: true});
    event_object.dispatchEvent(keyEvent);
}

event_object.addEventListener('keypress', function(e){

    //    This function listens to the event
    console.log(event, 'ctrlKey: ' + e.ctrlKey, 'shiftKey: '+ e.shiftKey, e.char, e.key);
    alert('CTRL + SHIFT + p pressed')

    e.preventDefault();

});

//    Just trigger CTRL + SHIFT + p with emitKeyEvent();
emitKeyEvent();

You can test it here: http://jsfiddle.net/u3gdQ/



来源:https://stackoverflow.com/questions/22099374/execute-key-press-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!