←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Converting KEYCODE into CHARACTER

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

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

View PostTechnicalCoding, on 22 June 2016 - 04:50 PM, said:

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 by TechnicalCoding, 22 June 2016 - 08:59 PM.
Quote

NanoBob's Photo NanoBob 22 Jun 2016

You'd probably want to use the char event instead of key.
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

Might be smart, but to notice the ENTER & BACKSPACE click what will the name be?
Quote

NanoBob's Photo NanoBob 22 Jun 2016

View PostTechnicalCoding, on 22 June 2016 - 04:03 PM, said:

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?
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

View PostNanoBob, on 22 June 2016 - 04:05 PM, said:

View PostTechnicalCoding, on 22 June 2016 - 04:03 PM, said:

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 by TechnicalCoding, 22 June 2016 - 04:09 PM.
Quote

NanoBob's Photo NanoBob 22 Jun 2016

View PostTechnicalCoding, on 22 June 2016 - 04:07 PM, said:

View PostNanoBob, on 22 June 2016 - 04:05 PM, said:

View PostTechnicalCoding, on 22 June 2016 - 04:03 PM, said:

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 by NanoBob, 22 June 2016 - 04:16 PM.
Quote

Xella's Photo Xella 22 Jun 2016

You can use

if key == keys.enter then
  ...
elseif key == keys.backspace then
  ...
end

or something like that.
Edited by Xelostar, 22 June 2016 - 04:16 PM.
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

You can use

if key == keys.enter then
  ...
elseif key == keys.backspace then
  ...
end

or something like that.

This do not work.. ;( sadly

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

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 by TechnicalCoding, 22 June 2016 - 04:24 PM.
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

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...
Quote

Emma's Photo Emma 22 Jun 2016

View PostTechnicalCoding, on 22 June 2016 - 04:20 PM, said:

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

You can use

if key == keys.enter then
  ...
elseif key == keys.backspace then
  ...
end

or something like that.

This do not work.. ;( sadly

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

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 by Incinirate, 22 June 2016 - 04:32 PM.
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

View PostIncinirate, on 22 June 2016 - 04:32 PM, said:

View PostTechnicalCoding, on 22 June 2016 - 04:20 PM, said:

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

You can use

if key == keys.enter then
  ...
elseif key == keys.backspace then
  ...
end

or something like that.

This do not work.. ;( sadly

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

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?
Quote

Xella's Photo Xella 22 Jun 2016

View PostTechnicalCoding, on 22 June 2016 - 04:20 PM, said:

View PostXelostar, on 22 June 2016 - 04:15 PM, said:

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... :/
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

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
Quote

Lyqyd's Photo Lyqyd 22 Jun 2016

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.
Quote

TechnicalCoding's Photo TechnicalCoding 22 Jun 2016

View PostLyqyd, on 22 June 2016 - 04:55 PM, said:

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..
Quote

MKlegoman357's Photo MKlegoman357 22 Jun 2016

View PostTechnicalCoding, on 22 June 2016 - 06:54 PM, said:

View PostLyqyd, on 22 June 2016 - 04:55 PM, said:

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.
Quote

TechnicalCoding's Photo TechnicalCoding 26 Jun 2016

View PostMKlegoman357, on 22 June 2016 - 09:09 PM, said:

View PostTechnicalCoding, on 22 June 2016 - 06:54 PM, said:

View PostLyqyd, on 22 June 2016 - 04:55 PM, said:

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 :)
Quote