Jump to content

Key detection stuck on?


Recommended Posts

Alright, I'm toying with trying to create a mod and I'm slowly working things out.

This is a very simple code that I'm using to learn that is supposed to print "Wake up call" when the flight scene starts and "Z pressed" when the Z key is pressed.

Total code is only 20 lines long.


using System;
using UnityEngine;


namespace TWR1
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class TWR1 : MonoBehaviour
{
public void Awake()
{
print("Wake up call");
}
public void Update()
{
if(Input.GetKeyDown(KeyCode.Z) == true);
{
print("Z pressed!");
}
}
}
}

Now, the "Wake up call" works, it prints once when the flight scene starts.

However, the debug log is continuously filling with "Z pressed!" even though the Z key is not pressed. It seems every update is evaluating that If statement to true somehow?

Pressing the Z key does nothing, the debug log keeps filling with "Z pressed!", so it's not a matter of the If statement being backwards.

I've now spend 3 hours trying to get this to print to the debug log only when the Z key is actually pressed and am out of ideas?

Help, please?

D.

Link to comment
Share on other sites

@Frement: That worked, THANK YOU!

Now that I know the error, I added that semi-colon because back before I had the IF statement syntax correct, Visual Studio was throwing a compile error with a missing semi-colon at that position.

I never removed it after getting the IF statement syntax correct as Visual Studio did not flag it as an error so I left it in thinking it was necessary.

Ah well, that's what learning experiences are for.

Thanks again Frement.

D.

Link to comment
Share on other sites

Good point, thank you.

I will probably end up using some IF x == false statements later on, but it's kind of pointless on GetKeyDown as that only fires once per key press.

Now back to figuring out how ConfigNode works....

D.

Link to comment
Share on other sites

if(!x) works? Good to know.

On the D. at the end of my posts, I started out on newsgroups which didn't have the name bar down the left side and so I signed my posts like that.

I continue to sign off e-mails with my name to this day so it carries over to my forum posting.

Also, how did you get quotes to work? Both Reply and Reply with Quote are broken for me on Firefox.

D.

Link to comment
Share on other sites

if(!x) works? Good to know.

On the D. at the end of my posts, I started out on newsgroups which didn't have the name bar down the left side and so I signed my posts like that.

I continue to sign off e-mails with my name to this day so it carries over to my forum posting.

Also, how did you get quotes to work? Both Reply and Reply with Quote are broken for me on Firefox.

D.

The quote button is broken for me too (using chrome), however if I right click it and hit "Open in new tab" then it works.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...