Regards,
Augustas
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.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.augustas656 said:-snip-
[Edit] And disregard what Abelatox said, that isn't how pullEvent works.
Whitecatblack
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.augustas656 said:-snip-
Whitecatblack
while true do
local eventType, arg1, arg2, arg3 = os.pullEvent()
if eventType == "char" then
...
elseif eventType == "key" then
...
end
end
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.Bubba said:What actually happens is that pressing a key triggers two events: the key event and the char event.
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
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
event = {os.pullEvent()}
if event[1] == "char" then
--#aha, a character
elseif event[1] == "key" then
--#guess its a key event
end
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.
yes it would, but generally people only deal with specific keys in the key event handling, exampleAlthough 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.
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.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.Why not just use keys.getName()?
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.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.
local tKeys = {
nil, "one", "two", "three", "four", -- 1
"five", "six", "seven", "eight", "nine", -- 6
"zero", "minus", "equals", "backspace","tab", -- 11
"q", "w", "e", "r", "t", -- 16
"y", "u", "i", "o", "p", -- 21
"leftBracket","rightBracket","enter","leftCtrl","a", -- 26
"s", "d", "f", "g", "h", -- 31
"j", "k", "l", "semiColon","apostrophe", -- 36
"grave", "leftShift","backslash","z", "x", -- 41
"c", "v", "b", "n", "m", -- 46
"comma", "period", "slash", "rightShift","multiply", -- 51
"leftAlt", "space", "capsLock", "f1", "f2", -- 56
"f3", "f4", "f5", "f6", "f7", -- 61
"f8", "f9", "f10", "numLock", "scollLock", -- 66
"numPad7", "numPad8", "numPad9", "numPadSubtract","numPad4", -- 71
"numPad5", "numPad6", "numPadAdd","numPad1", "numPad2", -- 76
"numPad3", "numPad0", "numPadDecimal",nil, nil, -- 81
nil, "f11", "f12", nil, nil, -- 86
nil, nil, nil, nil, nil, -- 91
nil, nil, nil, nil, "f13", -- 96
"f14", "f15", nil, nil, nil, -- 101
nil, nil, nil, nil, nil, -- 106
nil, "kana", nil, nil, nil, -- 111
nil, nil, nil, nil, nil, -- 116
"convert", nil, "noconvert",nil, "yen", -- 121
nil, nil, nil, nil, nil, -- 126
nil, nil, nil, nil, nil, -- 131
nil, nil, nil, nil, nil, -- 136
"numPadEquals",nil, nil, "cimcumflex","at", -- 141
"colon", "underscore","kanji", "stop", "ax", -- 146
nil, "numPadEnter","rightCtrl",nil, nil, -- 151
nil, nil, nil, nil, nil, -- 156
nil, nil, nil, nil, nil, -- 161
nil, nil, nil, nil, nil, -- 166
nil, nil, nil, nil, nil, -- 171
nil, nil, nil, "numPadComma",nil, -- 176
"numPadDivide",nil, nil, "rightAlt", nil, -- 181
nil, nil, nil, nil, nil, -- 186
nil, nil, nil, nil, nil, -- 191
nil, "pause", nil, "home", "up", -- 196
"pageUp", nil, "left", nil, "right", -- 201
nil, "end", "down", "pageDown", "insert", -- 206
"delete" -- 211
}