Jump to content




Converting KEYCODE into CHARACTER


16 replies to this topic

#1 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 03: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

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.


#2 NanoBob

  • Members
  • 102 posts

Posted 22 June 2016 - 03:59 PM

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

#3 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 04:03 PM

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

#4 NanoBob

  • Members
  • 102 posts

Posted 22 June 2016 - 04:05 PM

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?

#5 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 04:07 PM

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.


#6 NanoBob

  • Members
  • 102 posts

Posted 22 June 2016 - 04:15 PM

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.


#7 Xelostar

  • Members
  • 134 posts
  • LocationOn Earth

Posted 22 June 2016 - 04:15 PM

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.


#8 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 04:20 PM

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.


#9 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 04: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...

#10 Incinirate

  • Members
  • 216 posts
  • LocationDanktown, Memeland

Posted 22 June 2016 - 04:32 PM

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.


#11 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 04:39 PM

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?

#12 Xelostar

  • Members
  • 134 posts
  • LocationOn Earth

Posted 22 June 2016 - 04:47 PM

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

#13 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 04: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


#14 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,451 posts

Posted 22 June 2016 - 04: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.

#15 TechnicalCoding

  • Members
  • 35 posts

Posted 22 June 2016 - 06:54 PM

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

#16 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 22 June 2016 - 09:09 PM

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.

#17 TechnicalCoding

  • Members
  • 35 posts

Posted 26 June 2016 - 01:29 PM

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users