Skip to content

Commit 4268d87

Browse files
author
Monte Goulding
authored
Merge pull request livecode#6537 from montegoulding/keydown
[[ KeyIsDown ]] Implement key is down
2 parents f8fb840 + 8f6d018 commit 4268d87

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# LiveCode Builder Host Library
2+
## Engine library
3+
4+
* You can now use `the <modifierkey> is [currently] down` expression to
5+
determine if the shift, command, control, alt/option or caps lock keys
6+
are down. Use the optional `currently` adverb to differentiate between
7+
the shift key being down at the present moment and it being down when the
8+
current event occurred.
9+
10+
if the shift key is down then
11+
DoShiftKeyThing()
12+
end if

engine/src/engine.lcb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public foreign handler MCEngineEvalTheItemDelimiter(out rDelimiter as String) re
8383

8484
public foreign handler MCEngineEvalMyResourcesFolder(out pFile as optional String) returns nothing binds to "<builtin>"
8585

86+
public foreign handler MCEngineEvalKeyIsDown(in pKey as UInt8, in pEvent as CBool, out rState as CBool) returns nothing binds to "<builtin>"
87+
8688
/**
8789
Summary: Resolves a string to a script object.
8890
Object: The string describing the script object.
@@ -634,4 +636,30 @@ begin
634636
MCEngineEvalMyResourcesFolder(output)
635637
end syntax
636638

639+
/**
640+
Summary: Returns true if the key is down
641+
642+
Returns: The state of the key
643+
644+
Description:
645+
Use 'the ... key is down' to determine if the key was down at the start of the
646+
current event. Use 'the ... key is currently down' to determine if the key is
647+
down at the time it is being checked.
648+
649+
As in script, command and control keys return the state of the same key on non-macOS
650+
systems while on macOS they are separate keys. Additionally alt and option are
651+
different names for the same key.
652+
653+
*/
654+
syntax KeyIsDown is expression
655+
"the" ( "shift" <mKey=0> | \
656+
"command" <mKey=1> | \
657+
"control" <mKey=2> | \
658+
"alt" <mKey=3> | \
659+
"option" <mKey=3> | \
660+
"caps" "lock" <mKey=4> ) "key" "is" ( "currently" <mEvent=false> | <mEvent=true> ) "down"
661+
begin
662+
MCEngineEvalKeyIsDown(mKey, mEvent, output)
663+
end syntax
664+
637665
end module

engine/src/module-engine.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,45 @@ extern "C" MC_DLLEXPORT_DEF MCArrayRef MCEngineExecDescribeScriptOfScriptObject(
12801280

12811281
////////////////////////////////////////////////////////////////////////////////
12821282

1283+
extern "C" MC_DLLEXPORT_DEF void
1284+
MCEngineEvalKeyIsDown(uint8_t p_key, bool p_event, bool& r_down)
1285+
{
1286+
uint8_t t_modifier = 0;
1287+
switch (p_key)
1288+
{
1289+
case 0:
1290+
t_modifier = MS_SHIFT;
1291+
break;
1292+
case 1:
1293+
t_modifier = MS_CONTROL;
1294+
break;
1295+
case 2:
1296+
t_modifier = MS_MAC_CONTROL;
1297+
break;
1298+
case 3:
1299+
t_modifier = MS_MOD1;
1300+
break;
1301+
case 4:
1302+
t_modifier = MS_CAPS_LOCK;
1303+
break;
1304+
default:
1305+
r_down = false;
1306+
MCUnreachable();
1307+
break;
1308+
}
1309+
1310+
if (p_event)
1311+
{
1312+
r_down = (MCmodifierstate & t_modifier) != 0;
1313+
}
1314+
else
1315+
{
1316+
r_down = (MCscreen->querymods() & t_modifier) != 0;
1317+
}
1318+
}
1319+
1320+
////////////////////////////////////////////////////////////////////////////////
1321+
12831322
MC_DLLEXPORT_DEF MCTypeInfoRef kMCEngineScriptObjectDoesNotExistErrorTypeInfo = nil;
12841323
MC_DLLEXPORT_DEF MCTypeInfoRef kMCEngineScriptObjectNoContextErrorTypeInfo = nil;
12851324

0 commit comments

Comments
 (0)