Converting KEYCODE into CHARACTER
TechnicalCoding 22 Jun 2016
NOTE: I have made these variables shortened from term
c = term.setTextColor
b = term.setBackgroundColor
cp = term.setCursorPos
z = colors
while true do local event, key = os.pullEvent("key") if key == 28 then break elseif key == 14 then local xpos1, ypos1 = term.getCursorPos() if xpos1 > x then cp(xpos1 - 1, ypos1) b(z.black) write(" ") cp(xpos1 - 1, ypos1) end else write(tostring(key)) --- I want this to output the character that belongs to the keycode entered, this do only output the keycode end end
TechnicalCoding, on 22 June 2016 - 04:50 PM, said:
By adding this line after local e,key = os.pullEvent("key")
local k = keys.getName(key)then use "k" instead of key
which now means i can do
if k=="character" THEN
That is how i fixed the problem! Hope everyone can get a use of it!
Edited by TechnicalCoding, 22 June 2016 - 08:59 PM.
TechnicalCoding 22 Jun 2016
NanoBob 22 Jun 2016
TechnicalCoding 22 Jun 2016
NanoBob, on 22 June 2016 - 04:05 PM, said:
Edited by TechnicalCoding, 22 June 2016 - 04:09 PM.
NanoBob 22 Jun 2016
TechnicalCoding, on 22 June 2016 - 04:07 PM, said:
Here's the source code for the io api, which read() is in. http://pastebin.com/7EaCKMYE
Edited by NanoBob, 22 June 2016 - 04:16 PM.
Xella 22 Jun 2016
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
Edited by Xelostar, 22 June 2016 - 04:16 PM.
TechnicalCoding 22 Jun 2016
Xelostar, on 22 June 2016 - 04:15 PM, said:
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
This do not work.. ;( sadly
Xelostar, on 22 June 2016 - 04:15 PM, said:
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
I know how I could use read() function, but that would be too much code in one single line + a mess!
Edited by TechnicalCoding, 22 June 2016 - 04:24 PM.
TechnicalCoding 22 Jun 2016
Emma 22 Jun 2016
TechnicalCoding, on 22 June 2016 - 04:20 PM, said:
Xelostar, on 22 June 2016 - 04:15 PM, said:
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
This do not work.. ;( sadly
Xelostar, on 22 June 2016 - 04:15 PM, said:
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
I know how I could use read() function, but that would be too much code in one single line + a mess!
Also just FYI you could just use only key events by creating a reverse lookup table, it's actually very simple:
local lookup = {} for k,v in pairs(keys) do lookup[v] = k end
That way you can just do this:
e,key = os.pullEvent("key") local char = lookup[key] if char=="g" then --do stuff elseif char=="backspace" then --do other stuff end
Edited by Incinirate, 22 June 2016 - 04:32 PM.
TechnicalCoding 22 Jun 2016
Incinirate, on 22 June 2016 - 04:32 PM, said:
TechnicalCoding, on 22 June 2016 - 04:20 PM, said:
Xelostar, on 22 June 2016 - 04:15 PM, said:
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
This do not work.. ;( sadly
Xelostar, on 22 June 2016 - 04:15 PM, said:
if key == keys.enter then ... elseif key == keys.backspace then ... end
or something like that.
I know how I could use read() function, but that would be too much code in one single line + a mess!
Also just FYI you could just use only key events by creating a reverse lookup table, it's actually very simple:
local lookup = {} for k,v in pairs(keys) do lookup[v] = k end
That way you can just do this:
e,key = os.pullEvent("key") local char = lookup[key] if char=="g" then --do stuff elseif char=="backspace" then --do other stuff end
Xella 22 Jun 2016
TechnicalCoding 22 Jun 2016
By adding this line after local e,key = os.pullEvent("key")
local k = keys.getName(key)then use "k" instead of key
which now means i can do
if k=="character" THEN
Lyqyd 22 Jun 2016
TechnicalCoding 22 Jun 2016
Lyqyd, on 22 June 2016 - 04:55 PM, said:
MKlegoman357 22 Jun 2016
TechnicalCoding, on 22 June 2016 - 06:54 PM, said:
Lyqyd, on 22 June 2016 - 04:55 PM, said:
But this will not detect whether the letter written should be upper case or not (by holding shift button or caps lock). Also, numbers won't work. And if someone will be using a keyboard with a different layout then they won't be able to write anything. You should simply remove the event filter and use both: "key" and "char" events. Using the "key" event for backspace and enter and "char" event for any text characters. Oh, and there's also the "paste" event which you could add support for.
TechnicalCoding 26 Jun 2016
MKlegoman357, on 22 June 2016 - 09:09 PM, said:
TechnicalCoding, on 22 June 2016 - 06:54 PM, said:
Lyqyd, on 22 June 2016 - 04:55 PM, said:
But this will not detect whether the letter written should be upper case or not (by holding shift button or caps lock). Also, numbers won't work. And if someone will be using a keyboard with a different layout then they won't be able to write anything. You should simply remove the event filter and use both: "key" and "char" events. Using the "key" event for backspace and enter and "char" event for any text characters. Oh, and there's also the "paste" event which you could add support for.