Home › Forums › General XMetaL Discussion › how to work with key press event and write to task bar? › Reply To: how to work with key press event and write to task bar?
Reply to: how to work with key press event and write to task bar?June 29, 2010 at 6:49 pm
There is no OnKeyPress event so the only other solution would be to use the On_Update_UI event. This event fires very frequently (whenever the UI is updated), and perhaps frequently enough to run your script. See the Programmer's Guide for a more complete description.
To clarify this a bit more, as you don't have direct access to key presses (to capture any and all key presses) you may need to count the number of characters in the current element or whatever other context you need to tell the user about.
Perhaps something like this:
//XMetaL Script Language JScript:
var charCount = Selection.ContainerNode.Xml.length;
However, that will include surrounding tags, child tags and other things, including entities, comments, PIs, etc, which might not be what you want, so manipulating that further to selectively remove these other things might be necessary.
You might wish to have a look at the following “word count” script for inspiration on how to start stripping out unwanted things to give an accurate count:
Or you could use Range.Text instead after moving the Range to an appropriate location.
Whatever script you come up with would probably need to be put inside the On_Update_UI event if you need to constantly update what is displayed in the status bar.
An alternative, if you can restrict this character count reporting to specific elements only, would be to implement an XFT form or VB or other type of dialog for that element and have the user enter this text into a textbox that has the equivalent of an OnKeyPress or OnChange event and then do something similar to the above by getting the length of the equivalent of a “text.length” property for the control on the textbox control on the form.