Read keypress using JScript - keypress

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...

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.

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.

Related

Command-line to show a file's properties?

I want to write an app that displays the default Windows Properties sheet, I can't find much info on doing it the normal programmatic way, so I'm thinking maybe shell out to a command-line call; does anyone know the Windows command-line to call up a file's property/details window? Not having any luck searching for it.
This window here:
In a general sense, there are two methods for going about this. How you specifically accomplish it (i.e. PowerShell, C#, etc) is up to you.
Call the ShellExecute function on the file with the PROPERTIES verb.
Call the SHObjectProperties function on the file.
Examples of both can be found here in AutoIt: http://www.autoitscript.com/forum/topic/118673-open-a-files-properties-window/
It appears that there IS no direct command-line call that can do this, so I whipped up a quick little VBScript to do it, since it appeared to be the quickest and easiest way to achieve the end result (at least for my specific need).
The VBScript simply sends the Windows Shortcut Alt + Enter; so, when a file is highlighted it'll automatically use that file as the one to send the shortcut to.
For anyone else who may want to do the same, just paste the following into a new text file and save it as a .vbs file:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "%{ENTER}"
For anyone interested in an even better solution, I ended up writing a small command-line app in VB.NET to do this, here's a link to it's full info + the source code: https://geekdrop.com/x/props

How to get the main window handle of a process using JScript?

Is there any method in JScript to get the handle of the main window of a process by providing the process name? The Process.MainWindowHandle property works only in JScript .NET. Is anything similar available in classic JScript?
I am not sure if this works, just try to loop window.parent until its undefined.
something like -
var mainWindow = window;
while( mainWindow.parent ) {
mainWindow = mainWindow.parent;
}
you also have something like window.top which always returns you the topmost window. But not sure if this is supported by all browsers.
JScript and Windows Script Host don't have this functionality, and neither does WMI.
If PowerShell is an option for you, then you can use the Process.MainWindowHandle property you mentioned:
(Get-Process notepad).MainWindowHandle
Otherwise, you'll need to find or write an utility (COM object, command-line tool etc) that would provide this functionality, and call this tool from your script.
Edit: So you need to close the window — that's a UI automation task.
Windows Script Host provides very limited UI automation functionality. If you know the window title, you could try using the AppActivate to and SendKeys methods to activate that window and send the Alt+F4 shortcut to it. You can find an example this answer. (The code is in VBScript, but it should give you the idea.) However, this approach isn't reliable.
If you really really don't want to kill the process, the easiest solution is to use some third-party UI automation tool. For example, you could try the free AutoIt tool — I think it should be able to accomplish what you need.
Edit 2: Have you tried recording the closing of the window? You should get a script like this:
Sys.Process("notepad").Window("Notepad", "Untitled - Notepad").Close();
Isn't this what you need?
For a native win32 application, there is no such thing as a "main window". A process can have no windows at all, or several top level "main" windows.
Well once i had to write a add-in for Outlook. My boss wants a splash-screen to appear when Outlook loads. But Outlook window goes over the splash. After a lot of search i found FindWindow http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28FINDWINDOW%29%3bk%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29%3bk%28DevLang-CSHARP%29&rd=true this is help for it . This function finds window based on window caption and window class name. I p-invoked it and used it from C#. If you can use this function through JScript I think it could do the job for you. (I used Spy++ for finding lpClassName parameter)

How to show a popup without a browser

I need an "alert" type feature to troubleshoot an error. I am not using a browser and using javascript as windows administaration purposes. So is their a way to view a varibales value if I am not using a browser?
JScript is a scripting language based on the ECMAScript standard.
JScript is implemented as a Windows Script engine. This means that it can be plugged in to any application that supports the Windows Script host, such as Internet Explorer, Active Server Pages, etc. It also means that any application supporting Windows Script can use multiple languages — JScript, VBScript, Perl, and others.
For reasons that I am not sure about, but I believe it to be related to the fact the the DOM is not available outside the browser, the alert function is also not available outside the browser. In order to popup a dialog box to the user in this case you can use the following code:
WScript.Echo('The quick brown fox jumped over the lazy dog');
If you want a windows GUI popup, then:
var timeout = 0;
var buttons = 0; // OK
var icon = 48; // Exclamation
var shell = new ActiveXObject("WScript.Shell");
shell.Popup("text ...", timeout, "window title", buttons + icon);
and run your jscript program with the wscript command.
Microsoft JScript language reference.
Popup documentation.
On windows, you can use Windows Script Host to execute your javascript. It has a built in ability to do output, using Echo. There are some nuances though, since WSH uses jscript, not javascript, though the languages are similar.
A summary of the differences between WScript.Echo and WshShell.Popup:
Windows scripts (vbs, js, wsf etc.) can be run under one of two hosts: cscript.exe (command-line), and wscript.exe (graphical). Under cscript, WScript.Echo will produce a line of text in the console window. WshShell.Popup will always produce a message window, even under cscript.
WshShell.Popup lets you specify the buttons, title and icon type, like the VB/VBS MessageBox function. It also lets you specify how long the message should remain open.
WScript.Echo lets you pass multiple string arguments to output, and will print them separated with spaces.
You can create a simple file that will alert text that is passed to it, for example in python. I don't think there is any way to do this in Javascript though without a browser.
No with javascript. You can, using Visual Basic Script and MsgBox function. No need to install anything.
'In Hello.vbs. Comments starts with '
MsgBox "Hello there"
Look at HTA files. These file types allow you to run typical HTML/VBScript/JS code without the need for a browser specifically. Just rename your HTML file to an HTA extension and run it. IT will show your "page" and execute any JS necessary. This type of file will give you access to other WScript functions as well like creating Files or accessing AD if required.

How to have SlickEdit control another window?

I want to have SlickEdit control another window.
I have an idea of how this could be done using some window’s apis but I am not sure how to implement this in SlickEdit. I am assuming Slick-C (SlickEdit's macro language) would be used. I have done some limited coding in Slick-C but I am not sure if window apis can be run.
Here is what I want done using windows API.
BringWindowToTop (This will bring the other window to the top)
SetForegroundWindow
Simulate pressing the F7 in the other window by using SendKey.
SendKey is a method in WScript.Shell .
It sounds pretty straight forward, all I need to know is how to do it in SlickEdit.
Update:
I pretty much used the concept jussij outlined but in a language I am familar with called PL/B. I already had most of these APIs working for another process, so it was pretty easy to create this new program and had SlickEdit shell out to run the program.
Here is what was needed inside of SlickEdit:
_command BenShellSAV1P198() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_MDI_EDITORCTL|VSARG2_READ_ONLY)
{
save_all();
shell("T:\\Sunbelt\\CODE\\plbwin.exe -h -i PlbBenTDSm.INI SAV1P198.PLC","N");
}
Then I bound that macro to a hot key and it all works just fine.
SendKey is a method in WScript.Shell.
I am pretty sure everthing you describe can be done at the WScript level.
So you could write a script that does all the work and once you have it working, just add it as a tool to SlickEdit by running the script using the cscript.exe executable.

Customizing Windows Right-Click menus with multiple levels

I understand the process needed to customize a right click menu going through the regedit etc. However I need to the ability to go multiple levels such as in applications like WinZip. Here's a picture for clarification of what I need
alt text http://img14.imageshack.us/img14/9658/multiplemenus.jpg
You need to write a Shell Extension; there is a guide for writing one in managed code (C#) here. It will involve doing a bunch of interop and implementing COM interfaces that the windows shell will consume, namely IShellExtInit and IContextMenu.
However, one could argue that writing a Shell Extension in managed code is not advisable; it will force windows explorer to load the CLR, (or any app that uses the standard windows 'Open File' dialog) - native code (C++) would be a better choice for this.

Resources