Question

I am trying to do a simple JScript using the Windows Script Host (outside any browser, was my intent) to read users keypresses, but can't find a way to do what amounts to:

function tap(e) {
    ...code...
}
document.onkeypress = tap;

I'm no Windows systems programming guy (but a seasoned programmer) so I'm unfamiliar with ActiveX gadgets and services and the like. Doesn't WScript or WSShell provide a way to open a window (instead of the 'document')?

Or am I forced to run this from a browser to make it work? Seems overkill...

Was it helpful?

Solution

Windows Scripting Host doesn't provide any UI, well, not a GUI at least.

If you use CScript.exe then you can use StdIn, StdOut, etc. See Wscript.StdIn.Read method.

These are most of the objects available for Windows Scripting Host: main WSH objects, the dictionary and FileSystem objects come in handy: Dictionary and FileSystem objects

But if you want a GUI, then you can run an HTA file in MSHTA.exe and use HTML/CSS/JavaScript to handle your UI needs and still use the WSH objects.

Overkill? Well, list what you want from your UI. Now, you'd have to provide a mechanism for accessing all those features. And your example code shows you'd want to do it in a HTML-DOM-via-JavaScript-like manner. So, you'd need an HTML parser and DOM support. Looks like you want access to most of what a browser provides at that point.

OTHER TIPS

Try

var tap = function (e) {
  ...code...
}

document.onkeypress = tap;

I don't think the Windows Script Host provides an API for keyboard hooks. The most reliable way to do this may be to create a COM component that implements a keyboard hook (in C#/C++, for example) and use an instance of that object in JScript as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top