Hey, Scripting Guy! How can I cause a script to run inside an HTA any time a user presses ENTER?
Hey, JJ. Before answering your question we should state, for the record, that the Scripting Guys are unanimously opposed to the recent decision by the International Astronomical Union (IAU) to remove Pluto from the list of planets. (OK, technically the Scripting Guy who writes this column didn’t actually ask any of the other Scripting Guys for their opinion. But seeing as how the Scripting Guy who writes this column is always right, well, there’s very little chance they’d disagree with him, is there?)
Admittedly, some of this displeasure is personal in nature: having gotten an A in elementary school science, in part because he could name all nine of the planets, the Scripting Guy who writes this column is afraid someone will retroactively re-score all his tests and give him a B+ instead. More important, however, there’s little doubt that the IAU (which made this decision based on a vote in which only 5% of its members participated) are picking on Pluto simply because it’s smaller than all the other planets. What kind of message is that sending to the rest of the universe? To tell you the truth, IAU, we’re not impressed; why don’t you tell Jupiter or Saturn that they aren’t planets? Go ahead; we dare you.
That’s what we thought.
Besides, if we feel the need to get rid of a planet, we should be giving Uranus the boot, not Pluto. Not only does Uranus have a name that leaves every English-speaking grade school boy convulsing on the floor with laughter, but Uranus can’t even stand up straight. Instead, it actually floats through space lying on its side, effectively having an East and a West pole rather than North and South poles. Come on, IAU: you call that a planet? Sheesh.
Venus? Listen, don’t get us started on Venus.
Oh, well. In theory, we suppose that some of you might be more interested in knowing how to run a script any time a user presses the ENTER key than you are in hearing the Scripting Guys ramble on and on about the planet Pluto – excuse us: about the dwarf planet Pluto. If that’s the case then this little block of code should be of interest to you:
If window.event.Keycode = 13 Then
Msgbox “You pressed the Enter key.”
Note. Yes, we know: it’s not a very long script, is it? But that’s OK; unlike certain astronomical associations, the Scripting Guys won’t discriminate against something just because it’s not very big.
As you can probably tell, this is one of the least-useful HTML Applications (HTAs) you’ll ever see; that’s because it doesn’t actually do anything other than demonstrate how you can trap and respond to the ENTER key being pressed. But we figured that would be good enough: having seen how the basic idea works you should be able to transfer the concept to an HTA that does a little bit more than just pop up a message box each time you press ENTER. All you’ll have to do is replace our code (a single line that displays a message box) with your code. Is that OK with everyone? Excellent.
So how do we trap and respond to the ENTER key being pressed? To begin with, you might have noticed that the body of HTA consists entirely of a beginning and an ending <body> tag. Before you dismiss that, however, take a closer look at the onkeypress event added to the <body> tag:
What does onkeypress do? It does this: each time a user presses a key on the keyboard this event triggers a subroutine named RunScript. (And because the onkeypress event is attached to the <body> tag this happens regardless of which control, if any, has the focus at the time someone presses a key.)
Ah, good point: the only key we really care about is the ENTER key, isn’t it? To tell you the truth, we don’t know a straightforward way to trigger a subroutine if the ENTER key (and only the ENTER key) is pressed. Therefore we took the easy way out and we call the subroutine whenever any key on the keyboard gets pressed.
But don’t panic: that doesn’t mean a message box is going to pop up each time a user presses a key on the keyboard. Instead, take a look at the first line in the subroutine:
If window.event.Keycode = 13 Then
What we’re doing here is grabbing the window.event object (an object created any time an event – a key press, a mouse click, etc. – occurs in the HTA) and then examining the value of the Keycode property. It should come as no great shock that the Keycode property represents the Unicode key code associated with the keyboard key that triggered the event. Here we’re checking to see if the Keycode property is equal to 13, which just happens to be the value assigned to the ENTER key. If the Keycode equals 13 that means the user pressed ENTER; in turn, we display a message box to that effect. If the Keycode is anything but 13 we do nothing at all. Technically the subroutine gets called any time a key (any key) gets pressed on the keyboard. As far as the user experience goes, however, nothing ever happens unless the ENTER key gets pressed. To the user it looks like the subroutine is triggered only when he or she presses the ENTER key.
Sure, it’s a bit of a workaround. But as long as it performs the task we need it to perform, well, what difference does it make?
So how did we know that a Keycode value of 13 meant that ENTER had been pressed? We didn’t; that’s why we looked up Internet Explorer’s ISO Latin-1 Character Set. This is a good resource if you’re interested in responding to other keys and keystrokes; however, we’d encourage you to use it as soon as possible. After all, you never know when the IAU will decide that a, G, 6, w, and @ are no longer valid keys on the keyboard.
Anyway, JJ, this should get you started with your own HTA that runs a subroutine whenever someone presses ENTER. And, yes, you’re absolutely right: the planet Mercury was named after the Roman god of thieves. And the Roman god Saturn? He’s best known for eating his own children.
Just something we thought you – and the International Astronomical Union – should know