Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by kbkmn · Mar 29, 2020 at 02:59 PM · unity input

Getting number keys value with new Unity Input System

Hi. I'm trying to map weapon switching to number keys. I want to pass number key value to callback function, but i don't know how to get it or even if its possible alt text

Please help

screenshot-2020-03-29-at-175514.png (28.5 kB)
Comment
Add comment · Show 1
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 sacredgeometry · Mar 29, 2020 at 07:05 PM 0
Share

What do you mean? Surely you just need a mono behaviour to invoke an event if any of those keys are pressed no?

But that said why would you need to do that when all monobehaviours have access to consu$$anonymous$$g those events anyway using Input.GetKey and Input.GetKeyDown and the KeypadN values.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by ChipWhiteside · Jun 12, 2020 at 10:02 AM

@kbkmn Hello! I was running into the same issue and couldn't find it anywhere but after playing around a bit I came up with a solution. Our problem is that when you use the

"YourInputAction.YourActionMap.SwitchWeapon.performed += ctx => print(ctx.ReadValue())"

It prints out "1" no matter what button is pressed. My solution is on each of your action bindings (the 4 bindings under your "Switch Weapon") add a "Scale" under processors and make the scale factor the number key (1 for 1 key, 2 for 2 key, 3 for 3 key, etc). Then the ctx.ReadValue() will printed out 1 * factor which will be which number key you pressed!

I hope that helps!

alt text

alt text

,Hello! I was running into the same issue and couldn't find it anywhere but after playing around a bit I came up with a solution. Our problem is that when you use the

"YourInputAction.YourActionMap.SwitchWeapon.performed += ctx => print(ctx.ReadValue())"

It prints out "1" no matter what button is pressed. My solution is on each of your action bindings (the 4 bindings under your "Switch Weapon") add a "Scale" under processors and make the scale factor the number key (1 for 1 key, 2 for 2 key, 3 for 3 key, etc). Then the ctx.ReadValue() will printed out 1 * factor which will be which number key you pressed!

I hope that helps!

alt text

alt text


screen-shot-2020-06-11-at-50955-pm.png (80.7 kB)
screen-shot-2020-06-11-at-50935-pm.png (192.7 kB)
Comment
Add comment · Show 2 · 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 kbkmn · Jun 12, 2020 at 12:11 PM 0
Share

Thank you for your answer

avatar image Stygian65 · Apr 13, 2021 at 08:08 PM 0
Share

This is a neat little trick. You'd think that it would be easy to do this kind of thing. Does anyone know the recommended way to do this?

avatar image
2

Answer by MikeM1970 · May 07, 2021 at 12:00 AM

FWIW, if you already know that your keypress is coming from a number key (for example, you've bound this action to only the number keys, and not all the keys), then you can use int.TryParse on the ctx.control.name (which is a string).

(caveat: If you int.TryParse() a ctx.control.name that can't parse to an int, you'll still get 0, so your code might think you've hit the 0 key...)

  private void SetItemByKeyValue(UnityEngine.InputSystem.InputAction.CallbackContext ctx) {
 
             int numKeyValue; // the number key value we want from this keypress
 
             int.TryParse(ctx.control.name, out numKeyValue);
             // Warning! If ctx.control.name can't parse as an int, numKeyValue will be 0
 
             Debug.Log("int value of keypress is: " + numKeyValue);
 
             // Now do something with the key value ...
 
         }


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

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with FPS controller movement 0 Answers

Input not working on web player build. 1 Answer

Input System: How To Prevent Mouse From Auto-Switching Control Scheme? 1 Answer

Not getting input from joysticks unless it's 360 controller. 1 Answer

Integration of Developed Unity App into another developed Native iOS app as part of application by adding subview or any other way? 0 Answers


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