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 Reshad · Mar 26, 2014 at 02:07 PM · inputkeyboardevent-handling

Detect a specific Key Press Event Without Keyboard Input

For key press normally we use Input, but I want to call a specific key press event without giving input from keyboard. In details I want a key press (space) event running with a Boolean value. But that key press event is not be called from keyboard input.

 If (Input.GetKeyDown("space")) {
 
 //space key is pressed
 }

But I want:

 If(press == true) { //here press is Boolean
 
 //call space key is pressed event without key board input
 }

Is it possible in unity?

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 sandeepsmartest · Sep 25, 2015 at 12:01 PM 0
Share

Hi, Do you want to simulate keyboard keys with out taking input from user? or just to call a method(Code Snippet) ?

2 Replies

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

Answer by Jamora · Mar 26, 2014 at 02:24 PM

You need to take advantage of a programming concept called subroutines. They are also called functions, or methods and possibly several other names.

Define a function that does whatever a spacebar press should do, then call it whenever you need that action performed. Functions are defined in C# usually as

 < access modifier > < return value > < function name > (< parameters >){
 < method body >
 }

that means, for example

 public void SpacePressed(){/*empty method*/}

and in UnityScript as

 function < function name >(< parameters >)< : return value, optional >{
 < method body >
 }

so

 function SpacePressed(){/*empty method*/}

You call functions by their name:

 SpacePressed();
Comment
Add comment · Show 6 · 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 Reshad · Mar 26, 2014 at 02:33 PM 0
Share

But I want to call the space key is pressed event without checking the Input.Get$$anonymous$$eyDown("space") and without any keyboard input.

avatar image Jamora · Mar 26, 2014 at 02:39 PM 0
Share

You don't need any input to call methods. Just call them when you want in your code. You can call methods at any given moment, by writing the method's name. I gave an example in the last code snippet in my answer.

Basically, to solve your problem, just put the method call inside the if clauses in your question.

avatar image Reshad · Mar 26, 2014 at 02:52 PM 0
Share

Sorry I can't get you understand my question.Suppose I have no keyboard but I want to call Input.Get$$anonymous$$eyDown("space") event, then what can I do. In details I have a code where I set a boolean true when I press my space key.But I want unity to call space key press event automatically when I am not pressing space key in my keyboard.

avatar image Jamora · Mar 26, 2014 at 03:30 PM 0
Share

Because your boolean is the same thing as having pressed space bar, then just set the boolean true somewhere else, and it's the same thing as having pressed space bar.

To illustrate, you make a method that performs whatever your space press performs, and then call if within your two ifs.

Let's say you want to print "Space was pressed" every time space was pressed. Your method would be, in C#

 public void SpacePressed(){
     print("Space was pressed");
 }

And you'd call it as follows:

 If (Input.Get$$anonymous$$eyDown("space")) {
      SpacePressed();
 }
 
 If(press == true) { //here press is Boolean
     SpacePressed();
     press = false;
 }

Now, whenever you set press to true, it is the absolute same thing as pressing the Space-key, without ever pressing the Space-key. It doesn't matter where you have

 press = true;

it will be the same thing as having space pressed.

avatar image NCartist Jamora · Sep 25, 2015 at 12:17 AM 0
Share

This question doesn't really seem answered. What if the poster wanted to call the left or right keys virtually? Using a boolean function just because the space bar happens to be a boolean input doesn't seem to be the answer the poster is looking for.

avatar image Reshad · Apr 02, 2014 at 08:23 PM 0
Share

I think I can use System.Windows.Forms.Send$$anonymous$$eys.SendWait("{}"); for getting space key press event without pressing space key. But how can I do that inside unity?

avatar image
1

Answer by Chom1czek · Oct 04, 2015 at 02:53 PM

I have discovered it recently but maybe you would like to check the "event system". Basicly you can check the current event in OnGUI method like this:

 void OnGUI()
 {
     EventListener(Event.current);
 }
 
 void EventListener(Event e)
 {
   if(e.rawType == EventType.mousedown && e.button == 0) Debug.Log("Left click event handled");
 }

PS OnGUI() must be implemented to get Event.current!

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 meat5000 ♦ · Oct 04, 2015 at 06:07 PM 0
Share

This check for current events, correct?

OP wanted a way to emulate the keyboard and fire off a specific 'keyboard event' without the use of a keyboard.

avatar image Chom1czek meat5000 ♦ · Oct 04, 2015 at 06:36 PM 0
Share

Ohh I see but now it doesn't make any much more sense to me :D Thanks and my bad.

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

26 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

Related Questions

checking for keyboard input with exceptions 0 Answers

Unable to catch KeyCode.LeftShift Down AND Up 5 Answers

control key question 1 Answer

How to make floating mobile keyboard in mobile game ? 0 Answers

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