Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BitMiller · Jan 13, 2011 at 09:44 PM · intenumkeycodecastingas-operator

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BitMiller · Jan 14, 2011 at 06:00 PM 0
Share

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

avatar image Mike 3 · Jan 14, 2011 at 06:16 PM 0
Share

Test other values besides 1 in that case. Your code is fine as it is, but there isn't a key corresponding to 1

avatar image BitMiller · Jan 14, 2011 at 06:27 PM 0
Share

Ok $$anonymous$$ike, this way I accept YOUR answer - besides I figured it out for now... :) Thx anyway! :)

avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges