This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Jovaage's profile picture

Detecting multiple key presses at once

Started by Jovaage, 10 June 2014 - 09:46 PM
Jovaage #1
Posted 10 June 2014 - 11:46 PM
I'm writing an app where I need to rewrite the read() function.
This is because I don't want some of the behaviour in read(), and I also need to detect other events.
This means that I cant just os.pullEvent("char") as that will not allow me to detect enter or f-key presses.
And I cant use os.pullEvent("key") either, as then it will fire when I press shift if I want to type forward-slash.

So, is there a way I can detect if a key is being held down while another one is pressed?
If I could check if that key is down the same instance that another key goes off.
Is this at all possible?
Lyqyd #2
Posted 11 June 2014 - 01:02 AM
Nope. Though you should be using an unfiltered os.pullEvent and handling the different cases separately.
Bomb Bloke #3
Posted 11 June 2014 - 01:11 AM
Eg.
Jovaage #4
Posted 11 June 2014 - 02:57 AM
Thats the thing tho, If its unfiltered, I wont get a char event, only key events.
Bomb Bloke #5
Posted 11 June 2014 - 03:52 AM
You'll get both, eventually.

Every button which would generate a "char" event generates a "key" event beforehand. The idea is that, as you keep pulling events, you check each to see what type it is:

if you get a "key" event, check to see if it's a "return"/"backspace"/etc key. Act on it if it's one of those special keys, otherwise ignore the event - if a "key" event comes along with the value of "keys.s" for example, you do nothing.

If a "char" event comes along, simply add it to your string.
Jovaage #6
Posted 11 June 2014 - 04:03 AM
I fail to see how I would ever get a char event. it would fire at key all the time
When I press d and, I dont filter it will always return as a key.
theoriginalbit #7
Posted 11 June 2014 - 04:08 AM
definitely not. if you press d there will two events, the first will be a key event with the key code for d, the second event will be a char event with the character d. try this code and press keys to see what happens


while true do
  local event = { os.pullEvent() }
  print( table.concat( event, ', ' ) )
end
Jovaage #8
Posted 11 June 2014 - 04:24 AM
Hey, look at that.
Thanks!
Thats not very intuitive.
A section about pulling a char event when pulling unfiltered should probably be added to the wiki, or made more visible if it already exist somewhere.
MKlegoman357 #9
Posted 11 June 2014 - 01:12 PM
Yeah, I've seen many people complain about not getting 'char' event. That's probably because they are running this code inside the interactive Lua prompt:


> os.pullEvent()
key
45
>

I don't really think this should be mentioned on the wiki, it happens just because people don't really understand os.pullEvent correctly.
Bomb Bloke #10
Posted 11 June 2014 - 01:17 PM
I think it'd be logical to mention it on the pages dedicated to the char / key events, though I doubt anyone would find it there…
Jovaage #11
Posted 11 June 2014 - 04:18 PM
@MKlegoman357
I agree that people dont understand os.pullEvent, me included until recently.
However I think it should be mentioned in the wiki, because it didnt help me understand how it worked.
I didnt find anywhere on the wiki stating that some actions produce 2 events.