35 posts
Posted 22 June 2016 - 04:51 PM
Currently I have script which looks like this:
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
Answer found!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 on 22 June 2016 - 07:59 PM
105 posts
Posted 22 June 2016 - 04:59 PM
You'd probably want to use the char event instead of key.
35 posts
Posted 22 June 2016 - 05:03 PM
Might be smart, but to notice the ENTER & BACKSPACE click what will the name be?
105 posts
Posted 22 June 2016 - 05:05 PM
Might be smart, but to notice the ENTER & BACKSPACE click what will the name be?
You could use both events in that case. But instead couldn't you simply use the read() function?
35 posts
Posted 22 June 2016 - 05:07 PM
Might be smart, but to notice the ENTER & BACKSPACE click what will the name be?
You could use both events in that case. But instead couldn't you simply use the read() function?
No, this is some kind of read() function but my custom one, which allows me to give live replies to the user ex "Username is already in use" so they do not need to re enter the whole string. Also what is the names of enter and backspace?
Edited on 22 June 2016 - 03:09 PM
105 posts
Posted 22 June 2016 - 05:15 PM
Might be smart, but to notice the ENTER & BACKSPACE click what will the name be?
You could use both events in that case. But instead couldn't you simply use the read() function?
No, this is some kind of read() function but my custom one, which allows me to give live replies to the user ex "Username is already in use" so they do not need to re enter the whole string. Also what is the names of enter and backspace?
with the key event enter and backspace are 28 and 14 respectively. You could probably take a look at the original read function in computercraft to check how that works.
Here's the source code for the io api, which read() is in.
http://pastebin.com/7EaCKMYE
Edited on 22 June 2016 - 03:16 PM
147 posts
Location
On Earth
Posted 22 June 2016 - 05:15 PM
You can use
if key == keys.enter then
...
elseif key == keys.backspace then
...
end
or something like that.
Edited on 22 June 2016 - 03:16 PM
35 posts
Posted 22 June 2016 - 05:20 PM
You can use
if key == keys.enter then
...
elseif key == keys.backspace then
...
end
or something like that.
This do not work.. ;( sadly
You can use
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 on 22 June 2016 - 03:24 PM
35 posts
Posted 22 June 2016 - 05:28 PM
I have been looking into the read function, so what it tells me is that it is not possible to use read() and do live output if ex Username avaible, Username Taken, etc, cause everything assigned to it is self assigning…
218 posts
Location
tmpim
Posted 22 June 2016 - 05:32 PM
You can use
if key == keys.enter then
...
elseif key == keys.backspace then
...
end
or something like that.
This do not work.. ;( sadly
You can use
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 on 22 June 2016 - 03:32 PM
35 posts
Posted 22 June 2016 - 05:39 PM
You can use
if key == keys.enter then
...
elseif key == keys.backspace then
...
end
or something like that.
This do not work.. ;( sadly
You can use
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
Doing this made it output : nil, now what?
147 posts
Location
On Earth
Posted 22 June 2016 - 05:47 PM
You can use
if key == keys.enter then
...
elseif key == keys.backspace then
...
end
or something like that.
This do not work.. ;( sadly
It works for me though… :/
35 posts
Posted 22 June 2016 - 05:50 PM
Answer found!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
8543 posts
Posted 22 June 2016 - 05:55 PM
You could not use a filter when pulling events, allowing you to handle both char events for typed characters and key events for control keys. This is what the read function does.
35 posts
Posted 22 June 2016 - 07:54 PM
You could not use a filter when pulling events, allowing you to handle both char events for typed characters and key events for control keys. This is what the read function does.
I have tested this and it works perfectly… It gets the pressed key id/code and it finds the name of it..
1140 posts
Location
Kaunas, Lithuania
Posted 22 June 2016 - 10:09 PM
You could not use a filter when pulling events, allowing you to handle both char events for typed characters and key events for control keys. This is what the read function does.
I have tested this and it works perfectly… It gets the pressed key id/code and it finds the name of it..
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.
35 posts
Posted 26 June 2016 - 02:29 PM
You could not use a filter when pulling events, allowing you to handle both char events for typed characters and key events for control keys. This is what the read function does.
I have tested this and it works perfectly… It gets the pressed key id/code and it finds the name of it..
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.
True, but it is good enough for my use, I have also been adding support for the paste event :)/>