Jump to content




PullEvent, if Char elseif Key, still having issues


  • You cannot reply to this topic
8 replies to this topic

#1 augustas656

  • Members
  • 158 posts

Posted 04 June 2014 - 06:54 PM

I do os.pullEvent with no parameter in the brackets, how could I check if the event is a char do this action, and if it's a key do another action. With char being first priority because chars are also keys, and not all keys are chars. I just tried a simple if event == "char" then elseif event == "key" then end, these didn't work, how could I do this?

Regards,
Augustas

#2 Whitecatblack

  • Members
  • 49 posts
  • LocationUnited States (EST)

Posted 04 June 2014 - 07:13 PM

augustas656 said:

-snip-
With a small amount of testing, it seems that os.pullEvent() without any parameters, when a key is pressed, will default to event type "key" not "char". So if you want to pull a "char" event instead, you would have to give it that parameter and find a workaround to use it in your code.

[Edit] And disregard what Abelatox said, that isn't how pullEvent works.

Whitecatblack

Edited by Whitecatblack, 04 June 2014 - 07:14 PM.


#3 augustas656

  • Members
  • 158 posts

Posted 04 June 2014 - 07:15 PM

Abelatox it won't, event is a string value, key is a table and char is also a table, rather than a string that's set as important variables. I'll get an error string expected, got table.

View PostWhitecatblack, on 04 June 2014 - 07:13 PM, said:

augustas656 said:

-snip-
With a small amount of testing, it seems that os.pullEvent() without any parameters, when a key is pressed, will default to event type "key" not "char". So if you want to pull a "char" event instead, you would have to give it that parameter and find a workaround to use it in your code.

[Edit] And disregard what Abelatox said, that isn't how pullEvent works.

Whitecatblack

If I recall correctly, bios.lua file's read() function uses this same concept and somehow still manages to work around by using if "char" and if "key" checks. Wierd

Regards
Augustas

Edited by augustas656, 04 June 2014 - 07:15 PM.


#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 04 June 2014 - 07:17 PM

View PostWhitecatblack, on 04 June 2014 - 07:13 PM, said:

augustas656 said:

-snip-
With a small amount of testing, it seems that os.pullEvent() without any parameters, when a key is pressed, will default to event type "key" not "char". So if you want to pull a "char" event instead, you would have to give it that parameter and find a workaround to use it in your code.

Whitecatblack

Nope. os.pullEvent("char") simply waits until it gets the char event. What actually happens is that pressing a key triggers two events: the key event and the char event. If you were to use os.pullEvent() without arguments you would only get the first event which it receives - that would be the key event in most cases. In order to capture all events you'll want to set up a simple loop:
while true do
  local eventType, arg1, arg2, arg3 = os.pullEvent()
  if eventType == "char" then
	...
  elseif eventType == "key" then
	...
  end
end

Edited by Bubba, 04 June 2014 - 07:29 PM.


#5 Whitecatblack

  • Members
  • 49 posts
  • LocationUnited States (EST)

Posted 04 June 2014 - 07:49 PM

Bubba said:

What actually happens is that pressing a key triggers two events: the key event and the char event.
Wow, didn't know that. Although it seems it will always pull the key event first, then the char event, if it is a char event. So you would have to do something about that.

Whitecatblack

#6 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 04 June 2014 - 08:39 PM

Why not just use keys.getName()?
event = {os.pullEvent()}
if event[1] == "key" and keys.getName( event[2] ) then
 --#aha, a character
elseif event[1] == "key" and not keys.getName( event[2] ) then
--#guess its a key event
end

Edited by KingofGamesYami, 04 June 2014 - 08:41 PM.


#7 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 04 June 2014 - 08:46 PM

View PostKingofGamesYami, on 04 June 2014 - 08:39 PM, said:

Why not just use keys.getName()?
event = {os.pullEvent()}
if event[1] == "key" and keys.getName( event[2] ) then
 --#aha, a character
elseif event[1] == "key" and not keys.getName( event[2] ) then
--#guess its a key event
end

Because using 'key' events you cannot tell if the character pressed was capitalized or not.

EDIT: Also, it's shorter:

event = {os.pullEvent()}
if event[1] == "char" then
 --#aha, a character
elseif event[1] == "key" then
--#guess its a key event
end

EDIT2: And keys.getName returns names for not printable keys too, or in other words, for all keys, so in your example the first 'if' would never fail.

Edited by MKlegoman357, 04 June 2014 - 08:49 PM.


#8 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 04 June 2014 - 08:51 PM

View PostMKlegoman357, on 04 June 2014 - 08:46 PM, said:

View PostKingofGamesYami, on 04 June 2014 - 08:39 PM, said:

Why not just use keys.getName()?
event = {os.pullEvent()}
if event[1] == "key" and keys.getName( event[2] ) then
 --#aha, a character
elseif event[1] == "key" and not keys.getName( event[2] ) then
--#guess its a key event
end

Because using 'key' events you cannot tell if the character pressed was capitalized or not.

EDIT: Also, it's shorter:

event = {os.pullEvent()}
if event[1] == "char" then
 --#aha, a character
elseif event[1] == "key" then
--#guess its a key event
end

EDIT2: And keys.getName returns names for not printable keys too, or in other words, for all keys, so in your example the first 'if' would never fail.

If you read the OP, he already tried that.. Thanks for mentioning that bit about the key to char conversion, I didn't know that.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 June 2014 - 01:37 AM

View PostWhitecatblack, on 04 June 2014 - 07:49 PM, said:

Although it seems it will always pull the key event first, then the char event, if it is a char event. So you would have to do something about that.
yes it would, but generally people only deal with specific keys in the key event handling, example
local input = ""
while true do
  local event = { os.pullEvent() }
  if event[1] == "char" then
    input = input..event[2]
  elseif event[1] == "key" then
    if event[2] == keys.enter then
      break
    elseif event[2] == keys.backspace then
      input = input:sub(1, #input - 1)
    end
  end
end
which given what the above statements do it doesn't even matter what order your have the if ... elseif in.

View PostKingofGamesYami, on 04 June 2014 - 08:39 PM, said:

Why not just use keys.getName()?
as MKlegoman357 stated doing that will not tell you if the key is capital or not. and the other problem is that getName returns "one", "two", "space", "backslash" instead of "1", "2", " ", "\" so it returns the name of the key, not the printable character.

View PostMKlegoman357, on 04 June 2014 - 08:46 PM, said:

EDIT2: And keys.getName returns names for not printable keys too, or in other words, for all keys, so in your example the first 'if' would never fail.
not completely true, there are a lot of nil values in the table of names so not all the keys would skip the first if statement.
Keys Table






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users