Filter Out Key Repeat

Is there a built-in way to filter out repeated key events caused by holding down a key? I know it’s fairly easy just to check for down -> up -> down, but I’m just curious to know if I can skip the extra code.

  • Caleb

What I do is:
if event.phase == “up” then
print(“run”)
end

That works on my Windows computer, but on Mac it repeats the key event. With your code, I’d get:

run run (a second or so later, when the OS starts the key repeat event) run (after key delay) run run run run run run
  • Caleb

That is interesting, Are you use a the latest stable build? I would try to make a demo but my Mac is have trouble is 10.12 dev beta for Mac OS.

Yep, build 2016.2830. I’m simulating an iPad, but the same thing happens if I switch to a custom device with the same screen size.

  • Caleb

Whoops, just realized that I didn’t read your code completely. I only get one “up” phase. The problem is, I need to activate an event when the key is pressed, not released, and that’s what’s repeating. Sorry for the mixup.

  • Caleb

I don’t think there is a build-in way. But if your goal is to minimise the code, you could do something like this in main.lua, and just listen to the “nonRepeatedKey” events:

local pressedKeysMap = {} Runtime:addEventListener("key", function(event) local isAlreadyPressed = event.phase == "down" and pressedKeysMap[event.keyName] pressedKeysMap[event.keyName] = event.phase == "down" if not isAlreadyPressed then event.name = "nonRepeatedKey" Runtime:dispatchEvent(event) end end)

@Caleb, FWIW I do not see this behavior. If I use the below function:

local function onKey(event) print(event.keyName) print(event.phase) end Runtime:addEventListener("key", onKey)

I get one keyName per event phase. I tested on 2016.2899 on Windows 7. I do have the ability to repeat keystrokes with other text applications, so I do know that the behavior is there.

@pouwelsjochem8:

I already have a measure similar to that in my code. I was just wondering whether or not there was a built-in way to do it. A bit silly, I guess; after all, it is just a few lines. Still, I like to use as little “wrapper” code as possible.

@Alex@Panc:

It works perfectly on my Windows computer as well. It’s on my Mac that I have the problem.

  • Caleb

Sheesh, I ALWAYS have the problem of not reading closely enough. I just now saw where you mention that the behavior does not happen on Windows. Sorry for the useless post!

What I do is:
if event.phase == “up” then
print(“run”)
end

That works on my Windows computer, but on Mac it repeats the key event. With your code, I’d get:

run run (a second or so later, when the OS starts the key repeat event) run (after key delay) run run run run run run
  • Caleb

That is interesting, Are you use a the latest stable build? I would try to make a demo but my Mac is have trouble is 10.12 dev beta for Mac OS.

Yep, build 2016.2830. I’m simulating an iPad, but the same thing happens if I switch to a custom device with the same screen size.

  • Caleb

Whoops, just realized that I didn’t read your code completely. I only get one “up” phase. The problem is, I need to activate an event when the key is pressed, not released, and that’s what’s repeating. Sorry for the mixup.

  • Caleb

I don’t think there is a build-in way. But if your goal is to minimise the code, you could do something like this in main.lua, and just listen to the “nonRepeatedKey” events:

local pressedKeysMap = {} Runtime:addEventListener("key", function(event) local isAlreadyPressed = event.phase == "down" and pressedKeysMap[event.keyName] pressedKeysMap[event.keyName] = event.phase == "down" if not isAlreadyPressed then event.name = "nonRepeatedKey" Runtime:dispatchEvent(event) end end)

@Caleb, FWIW I do not see this behavior. If I use the below function:

local function onKey(event) print(event.keyName) print(event.phase) end Runtime:addEventListener("key", onKey)

I get one keyName per event phase. I tested on 2016.2899 on Windows 7. I do have the ability to repeat keystrokes with other text applications, so I do know that the behavior is there.

@pouwelsjochem8:

I already have a measure similar to that in my code. I was just wondering whether or not there was a built-in way to do it. A bit silly, I guess; after all, it is just a few lines. Still, I like to use as little “wrapper” code as possible.

@Alex@Panc:

It works perfectly on my Windows computer as well. It’s on my Mac that I have the problem.

  • Caleb

Sheesh, I ALWAYS have the problem of not reading closely enough. I just now saw where you mention that the behavior does not happen on Windows. Sorry for the useless post!