0

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

Community
  • 1
  • 1
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
  • Why do the keys have to be pressed as opposed to firing the key's events? – Zach Saucier Feb 28 '14 at 16:08
  • Do you mean why don't I get Javascript to open the till? If so the issue is that the webpage is external and the exe is internal. therefore I would need to execute the exe on the clients machine. – The Humble Rat Feb 28 '14 at 16:12
  • The webpage is "external" but it can send keystrokes to the client? – Justin Paulson Feb 28 '14 at 16:39
  • @JustinPaulson I have heard concerns that the Chrome sandbox will stop the command, so I am using chrome without the sandbox at the moment and the exe is still not launched when the keys are pressed, well, when the above code is used that is. – The Humble Rat Feb 28 '14 at 16:41

1 Answers1

3

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/

Mircea
  • 11,373
  • 26
  • 64
  • 95
  • If I change this to remove the shift for example so it is just CTRL+p, presumably this should bring up the print dialog box? I did try but nothing came up. – The Humble Rat Feb 28 '14 at 17:00
  • Well, this event is triggered by its document context. You need it triggered by the chrome context...there are more 'documents' in one browser – Mircea Feb 28 '14 at 17:05
  • Is it possible to trigger it on a global level, ie turning off the sandbox etc. – The Humble Rat Feb 28 '14 at 17:27
  • If I simulate CTRL+A in this way, is it supposed to select the text of the element? It is not doing it, although the alert is coming correctly. – SexyBeast Jan 31 '17 at 14:25