Friday, August 19, 2011

Servoy TIP: How To Trap For Modifier Keys

When you're developing your solution - there are times where you want to know if the user had any modifier keys pressed when they performed the action (drag and drop, right click, shift key down, etc.). Servoy returns a number for each key that is held down and, if you add up the numbers, you can tell which keys are down (much like my FileMaker Tip on the same subject)


Luckily, there's a really straightforward to accomplish this - and it's all held in the event that triggered your method.
if ((event.getModifiers() & JSEvent.MODIFIER_SHIFT) != 0) {
   application.output("shift")
} else if ((event.getModifiers() & JSEvent.MODIFIER_ALT & JSEvent.MODIFIER_CTRL) != 0 ) {
   appliction.output ("alt and ctrl");
}
The operator is a bit operator - that's why you have to use this type of syntax - but since there are event constants (under Application -> JSEvent in the Solution Explorer) - it's easy to check for whatever key combination you want!

You can also tell what type of action the user was doing (as well as any key combinations) - by using the event.getType() function. Here's an example:
if (event.getType() == JSEvent.ACTION) {
   application.output("user clicked");
} else if (event.getType() == JSEvent.RIGHTCLICK) {
   application.output("user right clicked");
} else if (event.getType() == JSEvent.DOUBLECLICK) {
   application.output("user double clicked");
}
There are other even type constants you can take advantage of including: action, datachanged, focuslost, focusgained, none (for unknown types), ondrag, ondragover, ondrop and rightclick.

No comments:

Web Analytics