1852 posts
Location
Sweden
Posted 31 August 2014 - 03:21 AM
Hello everyone, I've stumbled upon a problem, it's that I don't know how to detect if multiple keys have been pressed. Like you've might have seen in programs that they use CTRL+S or something. Now how would I do this? can someone give me some code example? do I have to modify os.pullEvent/pullEventRaw or something todo this?
I tried doing this in some weird way using timers, problem was I couldn't hold down a key. ( for example hold down shift, then press left. I had to press shift and then press left )
And if you're wondering if I have to code left to show I can tell you right now that the answer is nope. The code got deleted as it was a test-script and it didn't work.
- Thanks in advance, TheOddByte
108 posts
Posted 31 August 2014 - 03:42 AM
There's not really a way to do that in CC (though somewhere on the forums I've seen someone jury-rig it). The best bet is just to see if someone's pressed the two keys within some time of each other.
7083 posts
Location
Tasmania (AU)
Posted 31 August 2014 - 04:23 AM
There's one exception (and only one) that comes to mind - holding down Ctrl while pressing characters generates key events for those characters, but not char events.
This means that if you rig your script to queue a custom event whenever a key event is detected, then see whether a char event can be pulled before that custom event, you can tell whether a given character was held in conjunction with Ctrl or not.
1852 posts
Location
Sweden
Posted 31 August 2014 - 06:04 PM
Thanks for the help, I gave it a shot and this was the result.
Spoiler
local event, enabled, timeout, key_hold, key = {}
while true do
local e = { os.pullEvent() }
if e[1] == "key" then
if e[2] == 29 or e[2] == 42 then
enabled = true
for k, v in pairs( e ) do
event[k] = v
end
timeout = os.startTimer( .8 )
else
if enabled then
if key and e[2] == key then
key_hold = true
else
if key == nil then
key = e[2]
key_hold = true;
else
key = nil
key_hold = false
end
end
-- Handle multiple keys here later, just print them now for debug
print( event[2] .. " + " .. e[2] )
end
end
elseif e[1] == "timer" and e[2] == timeout then
if not key_hold then
event = {}
enabled = false;
end
key_hold = false;
timeout = os.startTimer( 0.1 )
end
end
It's not perfect, but it kinda works :P/>
Edited on 31 August 2014 - 04:08 PM