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

Why not '1' instead of 'one' in the keys API

Started by Ranguna, 27 October 2013 - 09:47 AM
Ranguna #1
Posted 27 October 2013 - 10:47 AM
Title: Why not '1' instead of 'one' in the keys API

So I've been messing around with this mod and I noticed that when I do getName(2) I get 'one' and it'd make more sence if it returned '1'.

I noticed this because I was coding some sort of number "writing" to variable that went like this:

local char={}
local _,k
repeat
	 term.clear()
	 print('Please type some numbers:')
	 print(table.concat(char))
	 repeat
		 _,k=os.pullEvent('key')
	 until tonumber(keys.getName(k))~=nil or keys.getName(k)=='backspace' or keys.getName(k)=='enter'
	 if keys.getName(k)=='backspace' then
		 char[#char]=nil
	 elseif keys.getName(k)~='enter' then
		 char[#char+1]=keys.getName(k)
	 end
until keys.getName(k)=='enter'
local final=tonumber(table.concat(char))

Basicly if what you typed was a number this'd record it into the variable 'char,' if you press enter then it'll save all the characters in 'char' into another variable called 'final' in number form, thing is I get nil when I try to transform any recorder number into a number type variable because lua doesn't identify 'one' as number and returns nil if I do tonumber('one').


I've looked in the 'keys' file inside the 'apis' folder and decided to modify it.
I belive that changing tKeys table to this would make everyone a little happier no ?

local tKeys = {
	nil,		 "1",		 "2",		 "3",		 "4",			-- 1
	"5",		 "6",		 "7",		 "8",		 "9",			-- 6
	"0",		 "-",		 "=",		 "backspace","tab",			-- 11
	"q",		 "w",		 "e",		 "r",		"t",			-- 16
	"y",		"u",		"i",		"o",		"p",			-- 21
	"leftBracket","rightBracket","enter","leftCtrl","a",			-- 26
	"s",		"d",		"f",		"g",		"h",			-- 31
	"j",		"k",		"l",		";",		"'",	-- 36
	"grave",	"leftShift","backSlash",		"z",		"x",			-- 41
	"c",		"v",		"b",		"n",		"m",			-- 46
	",",		".",		"/",		"rightShift","*",			-- 51
	"leftAlt",	"space",	"capsLock",	"f1",		"f2",			-- 56
	"f3",		"f4",		"f5",		"f6",		"f7",			-- 61
	"f8",		"f9",		"f10",		"numLock",	"scollLock",	-- 66	
	"numPad7",	"numPad8",	"numPad9",	"numPadSubtract","numPad4",	-- 71
	"numPad5",	"numPad6",	"numPadAdd","numPad1",	"numPad2",		-- 76
	"numPad3",	"numPad0",	"numPadDecimal",nil,	nil,			-- 81
	nil,		 "f11",		"f12",		nil,		nil,			-- 86
	nil,		nil,		nil,		nil,		nil,			-- 91
	nil,		nil,		nil,		nil,		"f13",			-- 96
	"f14",		"f15",		nil,		nil,		nil,			-- 101
	nil,		nil,		nil,		nil,		nil,			-- 106
	nil,		"kana",		nil,		nil,		nil,			-- 111
	nil,		nil,		nil,		nil,		nil,			-- 116	
	"convert",	nil,		"noconvert",nil,		"yen",			-- 121
	nil,		nil,		nil,		nil,		nil,			-- 126
	nil,		nil,		nil,		nil,		nil,			-- 131
	nil,		nil,		nil,		nil,		nil,			-- 136
	"numPadEquals",nil,		nil,		"cimcumflex","@",			-- 141
	"colon",	"_",		"kanji",	"stop",		"ax",			-- 146
	nil,		"numPadEnter","rightCtrl",nil,		nil,			-- 151
	nil,		nil,		nil,		nil,		nil,			-- 156
	nil,		nil,		nil,		nil,		nil,			-- 161
	nil,		nil,		nil,		nil,		nil,			-- 166
	nil,		nil,		nil,		nil,		nil,			-- 171
	nil,		nil,		nil,		"numPadComma",nil,			-- 176
	"numPadDivide",nil,		nil,		"rightAlt",	nil,			-- 181
	nil,		nil,		nil,		nil,		nil,			-- 186
	nil,		nil,		nil,		nil,		nil,			-- 191
	nil,		"pause",	nil,		"home",		"up",			-- 196
	"pageUp",	nil,		"left",		nil,		"right",		-- 201
	nil,		"end",		"down",		"pageDown",	"insert",		-- 206
	"delete"														-- 211
}
(Really ? the code tag transforms tabs into spaces, great, sorry for the mess)
Ranguna #2
Posted 29 October 2013 - 02:02 PM
Soo.. What's the benefit of having 'one' instead of '1' ?
spdkils #3
Posted 29 October 2013 - 02:13 PM
capture the char event instead if you're looking to "type", not the keys.

if you press 1, it fires 2 events… the key event of 2, and the char event of 1.

So, if you're after printable chracters, grab char, instead of key.
Ranguna #4
Posted 29 October 2013 - 02:37 PM
Oh I didn't know about that event but that brings up another question, why do we have a char event and a key event ?

Wouldn't one of them be enough ?
Wojbie #5
Posted 29 October 2013 - 02:41 PM
Example =
You press t - char="t" key=20
You hold Shift. You press t - char="T" key=20 (Also happends when Caps-Lock On)
You hold Ctrl. You press t - key=20 (there is no char event)
No idea what happens with Alt. Never tested it.
spdkils #6
Posted 29 October 2013 - 02:52 PM
<– Clueless as well.

However I assume because some keys are non-printable, and some keys are. So they just fire key press events, and if it was a printable character, they fire the char event too. I don't know the reasoning, but I'm sure there is a good reason.
Engineer #7
Posted 29 October 2013 - 04:16 PM
<– Clueless as well.

However I assume because some keys are non-printable, and some keys are. So they just fire key press events, and if it was a printable character, they fire the char event too. I don't know the reasoning, but I'm sure there is a good reason.
So you can interact with you whole keyboard! That's what the key event is about, the char even just put outs printable characters in the charset
Lyqyd #8
Posted 29 October 2013 - 05:33 PM
The key event is received first, by the way.

Essentially, if the button pressed can be printed, the char event will return the printable character, taking Shift into account. The key event returns a keycode denoting only which key was pressed, regardless of any modifier keys (with perhaps an exception for low-level remapping keys).
MKlegoman357 #9
Posted 30 October 2013 - 10:30 AM
Back to the topic:

If you want to get 1 from keyboard input you take char event, if you want to get one from keyboard input then you take key event and use keys.getName function. This way you can get same input but different output.
immibis #10
Posted 30 October 2013 - 07:21 PM
If it was 1, you'd have to use keys["1"] to get the ID, instead of keys.one
theoriginalbit #11
Posted 30 October 2013 - 11:17 PM
If it was 1, you'd have to use keys["1"] to get the ID, instead of keys.one
like keys["end"] :P/>