Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Filter Out Key Repeat
Started by Caleb P Jun 15 2016 07:38 AM

9 replies to this topic
keyboard repeat event
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#2

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,810 posts
  • Enterprise

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

[TOPIC: post.html]
#3

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

Scott Harrison

[GLOBAL: userInfoPane.html]
Scott Harrison
  • Corona Geek

  • 1,810 posts
  • Enterprise

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.

[TOPIC: post.html]
#5

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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



[TOPIC: post.html]
#7

pouwelsjochem8

[GLOBAL: userInfoPane.html]
pouwelsjochem8
  • Enthusiast

  • 96 posts
  • Corona SDK

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)


[TOPIC: post.html]
#8

Alex@Panc

[GLOBAL: userInfoPane.html]
Alex@Panc
  • Corona Geek

  • 1,733 posts
  • Corona SDK

@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.



[TOPIC: post.html]
#9

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

@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



[TOPIC: post.html]
#10

Alex@Panc

[GLOBAL: userInfoPane.html]
Alex@Panc
  • Corona Geek

  • 1,733 posts
  • Corona SDK

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!




[topic_controls]
[/topic_controls]