- Home /
Integer value casting to KeyCode enum in JS?
Version 1:
private var k : KeyCode;
function Start () { k = KeyCode.Backspace; print (k); //outputs: Backspace }
function Update () { if (Input.GetKey(k) print ("Got it"); }
Above one succeeds.
Version 2:
private var k : KeyCode;
function Start () { k = 1; print (k); //outputs: 1 }
function Update () { if (Input.GetKey(k) print ("Got it"); }
And it never succeeds.
Any trying with "as", (KeyCode) 1, [1] fails... Did I overlooked something?
Answer by Mike 3 · Jan 13, 2011 at 09:53 PM
There isn't a key corresponding to the value 1.
If you want to check the value for the 1 key on the keyboard:
var i : int = KeyCode.Alpha1; //the 1 on the top row of the keyboard
print(i); //won't print out 1, it'll be 38 i think
But the question remains - why're you trying to use ints anyway?
Thank you both $$anonymous$$ike and DaveA for the answers but I can't accept them because there's a misunderstanding.
I want to write a custom input manager. There's one at http://roidz.weebly.com/ In the code there's a part, where the author scans through the Joytick buttons in the $$anonymous$$eyCode enumeration, and he uses an INT in his FOR loop. If one button is pressed he says:
input$$anonymous$$ey[n] = ($$anonymous$$eyCode)joy$$anonymous$$;
where "input$$anonymous$$ey[n]" is an array of $$anonymous$$eyCode and joy$$anonymous$$ is the incremented FOR loop variable. But it's all in C#.
Is there a casting similar in JavaScript (UnityScript...) ?
Test other values besides 1 in that case. Your code is fine as it is, but there isn't a key corresponding to 1
Ok $$anonymous$$ike, this way I accept YOUR answer - besides I figured it out for now... :) Thx anyway! :)
Answer by BitMiller · Jan 14, 2011 at 06:24 PM
Huh, thank God, finally, I could close 4 Firefox each one with a minimum of 5 tabs. :) Originally I thought casting to KeyCode enumeration goes:
In the help listing the KeyCode enum the ID increments by one by line. But NO! There are left out integer values those aren't in the enum listing. Not every integer contained by KeyCode element values. This way
var k : KeyCode = 1;
print (k);
really outputs "1", BUT forexample
var k : KeyCode = 8;
print (k);
outputs Backspace.
I should know that... Now I do and never forget. :)
A little code at the end:
private var k : KeyCode; private var i : int;
function Start () { for (i = 0; i < 100; i++) { k = i; print(i + " as KeyCode: " + k); } }
I totally accept my answer. :) I hope it is useful for somebody else as well... Huh.
Answer by DaveA · Jan 13, 2011 at 09:54 PM
Are you trying to get the '1 key on the alphanumeric keyboard' or the '1 key on the number pad'? Or whatever key that the integer 1 is assigned to? My guess is, that 1 (the integer) is some key (if any) other than '1'. Different OS's may use different keyboard mappings, which is why you should always use the enum values if you can (Alpha1 or Keypad1)
Your answer
Follow this Question
Related Questions
Can't compare byte to byte? 2 Answers
C# Problem with assigning values arrays 1 Answer
Is There A Way To Cast On 1 Returning Value 2 Times? 5 Answers
Why can't I animate an int or enum? 1 Answer
Another Error - Enum.Parse 1 Answer