How to check modifier keys in a more readable way

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

How to check modifier keys in a more readable way

Postby mboegem » Thu Jul 24, 2008 10:31 am

Remembering syntax for this kind of actions is not my strongest point.
My solution: create a global method which you can call easy and a boolean in return.

//get the last key modifiers of last user action (shift = 1,ctrl = 2,meta = 4,alt = 8 )

var $key = arguments[0]
var m = application.getLastKeyModifiers();


switch($key)
{
case 'shift' :
var i = 1;
break;

case 'ctrl' :
var i = 2;
break;

case 'meta' :
var i = 4;
break;

case 'alt' :
var i = 8;
break;

default:
var i = 'error';
}


return (m & i) == i;
Marc Boegem
Solutiative / JBS Group, Partner
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image

Partner of Tower - The most powerful Git client for Mac and Windows
User avatar
mboegem
 
Posts: 1742
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Postby david » Mon Jul 28, 2008 8:53 pm

There is an offset difference between mac and PC which you can trap for:

Code: Select all
var   keyPressed = application.getLastKeyModifiers()

if (keyPressed >= 16) { //offset for pc/mac values
   keyPressed -= 16
}
...
David Workman, Kabootit

Image
Everything you need to build great apps with Servoy
User avatar
david
 
Posts: 1727
Joined: Thu Apr 24, 2003 4:18 pm
Location: Washington, D.C.

Postby SteveInLA » Tue Jul 29, 2008 5:51 pm

Thanks for this, Marc. Here is a variation that accommodates multiple modifier keys. Pass it a string of mod key names delimited with the | character. It works for just a single mod key too:
Code: Select all
var vKey = arguments[ 0 ];
var vKeyArray = vKey.split( "|" );
var m = application.getLastKeyModifiers();
var i = 0;

if ( plugins.it2be_tools.arrayContains( vKeyArray, "shift" ) )
{
   i += 1;
}

if ( plugins.it2be_tools.arrayContains( vKeyArray, "ctrl" ) )
{
   i += 2;
}

if ( plugins.it2be_tools.arrayContains( vKeyArray, "meta" ) )
{
   i += 4;
}

if ( plugins.it2be_tools.arrayContains( vKeyArray, "alt" ) )
{
   i += 8;
}

if ( i == 0 )
{
   i = "error";
}

//offset for pc/mac values
if ( m >= 16 )
{
   m -= 16;
}

return ( m & i ) == i;

Steve in LA
SteveInLA
 
Posts: 233
Joined: Thu Jul 29, 2004 12:00 am
Location: Southern Oregon, USA

Postby SteveInLA » Tue Jul 29, 2008 7:50 pm

Oops. My method doesn't exactly work for multiple keys if you evaluate with a bitwise operator. It will work if you use:
Code: Select all
return m == i;

instead of
Code: Select all
return (m & i) == i;

If I only want to return a true value in the case when a single key is pressed and not a combination of keys that include the single key, is there any reason why a bitwise operator is needed?

Steve in LA
SteveInLA
 
Posts: 233
Joined: Thu Jul 29, 2004 12:00 am
Location: Southern Oregon, USA


Return to How To

Who is online

Users browsing this forum: No registered users and 7 guests

cron