Can a script find out with what keystroke it was invoked?

dust wrote on 7/12/2004, 10:23 AM
I plan to implement several different functions into a script that the user can choose from. Implementing a GUI is too slow for the user though, so the different functions should be assigned to different keystrokes. Until now I used the way to "encode" the needed script function into its filename (xx_1.js, xx_2.js, etc), which were identical copy of the same file. The script could then read its filename and decide about the needed functionality in a switch statement. The problem about this approach is tht you need to make many copies of the same file under different filenames, which is ugly (mainly if you still debug your program).

It would be very nice if you could assign the SAME script to SEVERAL keystrokes (using Preferences/Keyboard), and then within the script read out the actual keystroke the user chose to invoke the script and so differ between the operations. So my question is if there's a way to do that, some global variable (like ScriptFile) that the user could read to find out about it?

Comments

jetdv wrote on 7/12/2004, 11:35 AM
Umm... I hate to ask this:

If the script is going to do different things if different keys are pressed, why not just have different scripts assigned to each key??? And how would it be affected if assigned to a button instead???
dust wrote on 7/12/2004, 10:21 PM
In order not to have to change all scripts (around 12 in my case) if you need to change somethng in the common internals of the scripts. It is a script that implements hierarchical/nested grouping of events, so functions like "group","ungroup","extract group", "level up/down/left/right" etc need all to be implemented (I'll post about it as soon as it's finished). Also, it is much cleaner and easier to distribute if you have only _ONE_ file that can be assigned to 12 keystrokes than _TWELVE_ different files.

OK, I know I can put the whole common internals (30KB of text in my case) to a precomiled .dll and just put the calling functions into the scripts, which is what I'll probably do. Still, it would be nice if you could find out about the context of how the script was called. Guess it is not possible, but I had to ask :-)
jetdv wrote on 7/13/2004, 6:41 AM
It WOULD definitely be nice to have some mechinism for passing a parameter value. I really do have a similar situation in which I have 9 copies of the same script with ONE line of code changed between each one.
rcampbel wrote on 7/15/2004, 8:57 AM
There are ways for a script to determine what keys are currently pressed. For example, the Control.ModifierKeys property will provide the state of the control key modifiers (SHIFT, CTRL, ALT):

if (Control.ModifierKeys == (Keys.Control | Keys.Alt))
MessageBox.Show("Alt and Ctrl are pressed");

There are other methods including using the Windows GetKeyState API calls. For a good FAQ on this subject check out:

http://www.syncfusion.com/FAQ/WinForms/FAQ_c46c.asp#q993q

In order for these methods to work, the user will have to hold down the key until the script is actually running.

Randall