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
1
Question by zerophase · Feb 17, 2016 at 01:34 AM · inputdebugmultithreading

Multithreaded Input Detection

I'm working on a project that uses a Bluetooth signal sending data on the amount of force detected by a sensor. We're using that to move our character. We pushed all of the reading from the sensor to another thread to significantly cut down on a bottleneck in our code.

An issue came up where if other developers didn't have the sensor attached they wouldn't be able to test their changes to the project. So, I wrote up a mock object that gets used when no sensor is available. The only thing is I'd prefer to be able to use keypresses to simulate different input speeds, without needing the sensor. The only thing is Input.GetKey can only be called on the main thread. Since this is only used in the editor, and it wouldn't make sense to chanage the ReadData() interface just for debugging is there anyway to just use plain C# to detect keypresses on other threads?

Everything I've found on it seems to require Windows Forms, and I don't think it's the best idea to add that assembly to the project.

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

2 Replies

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

Answer by Bunny83 · Feb 17, 2016 at 02:38 AM

Well, when you are on windows you can always use the win API directly. Either GetKeyState or GetKeyboardState. If you don't know how to use an external native code method, take a look at pinvoke.net(GetKeyState) (GetKeyboardState).

If you are on a different system, i doubt there's a general solution without using Windows Forms of something like that. So for Mac and Linux you have to find equivalent native methods.

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 zerophase · Feb 17, 2016 at 03:42 AM 0
Share

@Bunny83 One thing I noticed is that even works outside of the window. Is there another API available for trapping the key to a window?

avatar image Bunny83 zerophase · Feb 18, 2016 at 12:07 AM 0
Share

Well, you can use the OnApplicationFocus callback to set a boolean flag if the application has the focus or not.

avatar image
1

Answer by zerophase · Feb 17, 2016 at 03:40 AM

This is the code I ended up using, in case anyone else needs a quick reference.

 [DllImport("USER32.dll")]
         static extern short GetKeyState(VirtualKeyStates nVirtKey);
 
         public bool IsKeyPressed(VirtualKeyStates testKey)
         {
             bool keyPressed;
             short result = GetKeyState(testKey);
             switch (result)
             {
                 case 0:
                     // Not pressed and not toggled on.
                     keyPressed = false;
                     break;
 
                 case 1:
                     // Not pressed, but toggled on
                     keyPressed = false;
                     break;
 
                 default:
                     // Pressed (and may be toggled on)
                     keyPressed = true;
                     break;
             }
             return keyPressed;
         }
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 Bunny83 · Feb 18, 2016 at 03:07 AM 0
Share

This would do the same ^^:

 public bool Is$$anonymous$$eyPressed(Virtual$$anonymous$$eyStates test$$anonymous$$ey)
 {
     return (Get$$anonymous$$eyState(test$$anonymous$$ey) & 0x8000) != 0;
 }

Get$$anonymous$$eyState returns a bitflag where the lowest bit indicates if the key is toggled and the highest bit if the key is currently down.

 // 0000 0000 0000 0000  -- a short value has 16 bits
 // |                 |
 // |                 \_____ toggle bit 0x1
 // \_______________________ down bit 0x8000

avatar image Sinci1 Bunny83 · Aug 18, 2017 at 01:21 AM 0
Share

Please help, i need this but "The type or namespace name "Virtual$$anonymous$$eyStates" could not be found. are you missing a assembly refrence?" i don't know which assembly refrence i need, please help :C thanks

avatar image Bunny83 Sinci1 · Aug 18, 2017 at 07:46 AM 0
Share

If you just read my answer you should find this link. Virtual$$anonymous$$eyStates is just an enum. It's part of the win API but not shipped with .NET so you have to define it yourself.

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

40 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

Related Questions

Debug iPhone game without smartphone. 1 Answer

Input Button is not setup Error 1 Answer

Debugging tools that can be used with Unity in Native OS and multithreading envi? 0 Answers

GetKeyDown not working,I can't destroy self object when I press a button using the Input.GetKeyCode , I'm new to unity so please help! 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 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