This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TechnicalCoding's profile picture

Converting KEYCODE into CHARACTER

Started by TechnicalCoding, 22 June 2016 - 02:51 PM
TechnicalCoding #1
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
NanoBob #2
Posted 22 June 2016 - 04:59 PM
You'd probably want to use the char event instead of key.
TechnicalCoding #3
Posted 22 June 2016 - 05:03 PM
Might be smart, but to notice the ENTER & BACKSPACE click what will the name be?
NanoBob #4
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?
TechnicalCoding #5
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
NanoBob #6
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
Xelostar #7
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
TechnicalCoding #8
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
TechnicalCoding #9
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…
Emma #10
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
TechnicalCoding #11
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?
Xelostar #12
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… :/
TechnicalCoding #13
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
Lyqyd #14
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.
TechnicalCoding #15
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..
MKlegoman357 #16
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.
TechnicalCoding #17
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 :)/>