Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
9
Question by kei2332th · Sep 28, 2010 at 04:53 PM · keypress

Simulating key presses

I want to use HTML buttons to make a character in Unity walk around in an environment by pressing the HTML buttons. I know how to communicate between HTML and Unity already, and I basically plan to use onMouseDown on the HTML button to update a UNITY boolean to true, and then the update function will execute code while the variable is true. And onMouseUp will change the variable back to false, stopping the update function from running that code. The problem is that I cannot figure out how to simulate pressing and holding down keyboard keys through code. I am using javascript in Unity and the character already walks using the arrow keys or w,a,s,d keys. Can anyone tell me how to simulate keyboard key presses through code? Any help would be greatly appreciated. Thanks in advance.

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
15
Best Answer

Answer by Jean-Fabre · Nov 18, 2010 at 09:59 AM

Hi,

It is possible to simulate keypress and any inputs really if you wrap all your calls to a class you built yourself. I do that always anyway, not for simulating but for allowing several different types of input to affect a single action in my application, and to have a lot more controls without affecting my game, for example depending on the context, I might want to disable all inputs or only some of them, etc etc.

so instead of querying

function Update () {
if (Input.GetKeyDown ("space"))
print ("space key was pressed");
}

you will now call

function Update () { 
if (InputBroker.GetKeyDown ("space")) 
print ("space key was pressed"); 
}

Where InputBroker is a script you wrote yourself with a static function GetKeyDown

By having this level of indirection, you can now call your InputBroker and force it to simulate a keypress or something else.

InputBroker.SetKeyDown ("space");

in InputBroker, you register space to be down for a frame in a private hash variable forcedKeyDowns or something.

then in your getKeyDown for InputBroker you would have something like

static function GetKeyDown(aKey){

 return Input.GetKeyDown(aKey) ||  _forcedKeyDowns.Contains(aKey) ;

}

This is an idea, if you need a working code, I can spend a little more time on this, no problem.

Hope it helps,

Jean

Comment
Add comment · Show 8 · 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 by0log1c · Apr 29, 2011 at 03:02 PM 0
Share

Ahh, I'll start using this solution. Simple, yet terribly effective ^^

avatar image dansav · Oct 22, 2015 at 11:22 PM 1
Share

Is there an example of this somewhere. I don't know who to believe dreamora or you. I don't see anyway to simulate a keypress in unity, so wouldn't it have to come from the system?

avatar image meat5000 ♦ dansav · Oct 22, 2015 at 11:26 PM 0
Share

$$anonymous$$eyboard events simply throw out a $$anonymous$$eyCode. Just bypass the event and use the keycode directly. Ultimately, you need to write code to respond to this anyway, so cut out the middleman.

avatar image dansav meat5000 ♦ · Oct 24, 2015 at 06:50 PM 0
Share

I'm confused, doesn't this require a keyboard event to begin with. How do you generate the keyboard event in the first place without the keyboard.

avatar image shrimpp0123 · Feb 15, 2016 at 07:06 PM 1
Share

Hi $$anonymous$$,

Would you be able to post some working code? It would be very helpful.

avatar image meat5000 ♦ shrimpp0123 · Feb 15, 2016 at 10:36 PM 1
Share

What $$anonymous$$ is suggesting is that you can have it set up such that one input will come back with information from another if you so choose.

You can either Generate a $$anonymous$$eyCode or simply skip to the output.

The actual "Event Generation" is what Dreamora refers to as Not Possible.

This little snippet can demonstrate

 void Update () {
     if(Input.Get$$anonymous$$ouseButton(0))
     {
         my$$anonymous$$ey = $$anonymous$$eyCode.G;
         Debug.Log("Click handled   " + my$$anonymous$$ey);
     }
     if(Input.Get$$anonymous$$ey(my$$anonymous$$ey))
     {
         Debug.Log("GGGGGGGGGGGGGGGGGG");
     }
 }

A Left $$anonymous$$ouse Click generates a $$anonymous$$eyCode.G and places it in a variable. See how Debug chucks out a G. This level of emulation is easy.

The next line Get$$anonymous$$ey(my$$anonymous$$ey) will NOT fire as the event was not generated through the Event system, however, pressing G on the keyboard still fires the event as my$$anonymous$$ey = $$anonymous$$eyCode.G;

I would like to investigate further when I have more time. I tried the Windows.Input $$anonymous$$eyConverter but "$$anonymous$$ethod not implemented" was my greeting after I'd set up the namespace.

avatar image Czar-Man meat5000 ♦ · Feb 17, 2017 at 03:37 PM 0
Share

I've tried to do something like this by simulating Delete when I press the left key, but it doesn't seem to work. It just moves left in the InputField.

avatar image artuom213 · Oct 15, 2021 at 04:18 AM 0
Share

This code doesn't work for me. Could you please give your InputBroker? For example

avatar image
-2

Answer by Dreamora 1 · Oct 04, 2010 at 02:12 AM

it is not possible to simulate keypresses.

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
-2

Answer by Snownebula · Jul 18, 2015 at 08:08 AM

SetKeyDown? This does nothing.

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

6 People are following this question.

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

Related Questions

proximity, keypress and mouse lock help please :) 1 Answer

Basic animation key press 0 Answers

How To Activate/Deactivate Image Effect On Keypress? 3 Answers

Need to press key twice to react 1 Answer

Scoring system by pressing a series of particular keys 2 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