Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: justifun on July 25, 2011, 03:29:48 PM

Title: Get Any Key Store Result as String
Post by: justifun on July 25, 2011, 03:29:48 PM
Modification to the Get any Key Action
Allows you to store the result of which button was pressed in a string variable

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Input)]
[Tooltip("Sends an Event when the user hits any Key or Mouse Button.")]
public class AnyKeyStoreString : FsmStateAction
{
[RequiredField]
public FsmEvent sendEvent;
[UIHint(UIHint.Variable)]
public FsmString storeResult;

public override void Reset()
{
sendEvent = null;
storeResult = null;
}

public override void OnUpdate()
{
storeResult.Value = Input.inputString;
if (Input.anyKeyDown)
Fsm.Event(sendEvent);
}
}
}
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 04, 2013, 04:26:16 AM
I just got round to trying this. Want to make some kind of text input system. But this action didn't seem to work.

It just works like the AnyKey action it's based on (doesn't store a string, just triggers the event).
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 04, 2013, 08:44:30 AM
Hi,

 it works for me, I just tried.

bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 04, 2013, 09:18:50 PM
Then I must be either missing something or I misinterpreted the use of this action.

You can see from these pictures my resulting built string is ---. I assumed it would be h-h-h- since that's the key I pressed.
Title: Re: Get Any Key Store Result as String
Post by: justifun on October 04, 2013, 09:57:44 PM
Correct me if im wrong jean, but build string requires you give it Arrays of strings for each element, not actual string variables.

so that's why they are blank in the output.

Title: Re: Get Any Key Store Result as String
Post by: justifun on October 04, 2013, 10:21:32 PM
Hrrm seems i'm getting the same error as @FractalCore now.  Its not saving the string anymore.

I wrote this a long time ago, did something change in playmaker that could be breaking it Jean?
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 07, 2013, 02:45:10 AM
Hi,

It's all working wheel here. I'll build a small example. Please bump me towards the end of the week.

bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: MABManZ on October 08, 2013, 05:34:32 AM
FYI this is the same issue I was having in the action request forum, but kinda gave up for the time being and wanted to come back later. Glad to see I'm not going crazy
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 08, 2013, 07:11:22 AM
Hi,

 Here is a working sample.

 Let me know if this is what you need.

Bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 08, 2013, 07:56:46 AM
Thanks.

Using your demo, single key presses still didn't work. So I mashed a whole lot of keys at once and got some results. Then went back and tried my own scene again.

Sure enough mashing many keys at once will register one or two keys. A single press does nothing. The same results in your scene and mine, so I'm using it right.

Weird. It's as if it's taking the input and then clearing it again in a single frame, too fast to see. And many simultaneous keys clog it up and the last few pressed will be kept. That's my novice explanation.

I got the same results in Playmaker 1.6.0 and 1.7.5 and in Unity 3.5.7, 4.0.0, and 4.2.0, all in Windows 7 64Bit.
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 08, 2013, 08:05:20 AM
Hi,

 there is a serious issue here, cause, this scene works perfectly here. everytime I press a key, it works. I can type my name just fine.

 Can you make a screencast of what you are experiencing?

you are likely set up right, something may be wrong in your OS or within Unity install on your computer or certain kinds of setup.

 what keyboard do you have? what computer?

Bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 08, 2013, 08:08:59 AM
Tested on two different machines, one with a really old keyboard (PS/2) and the other with a CMStorm Quickfire gaming keyboard (USB2) I bought on Saturday.
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 08, 2013, 08:13:33 AM
Ok, I just built and ran it. It works that way. Just not in the editor.

Any idea why that might happen?
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 08, 2013, 02:49:57 PM
Hi,

 no idea... but something is definitly wrong. I am on mac so maybe the unity has a glitch on windows. Maybe other will perhaps confirm this.

bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: MABManZ on October 09, 2013, 03:34:44 AM
Ran your sample scene and the same problem occurs (clears the key within 1 frame), but I'm on the same setup as FractalCore (Windows 7 64-bit), sorry don't have a Mac to try unfortunately.
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 09, 2013, 04:12:18 AM
Now, I wonder if justifun made the action on a Mac in the first place, and if he tested it recently on PC...

After testing it by playing builds (only way to get it working), I made a very handy little text input system. All the letters/numbers/symbols of course, got backspace erasing one letter back at a time. Saved as a string to use anywhere else. All thanks to this Action. It should really be included as standard.
Title: Re: Get Any Key Store Result as String
Post by: justifun on October 09, 2013, 05:49:24 AM
@FractalCore - nope, it was made on a PC.  It was just a copy of the "Any Key" action with a line added to store the result.


Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 09, 2013, 05:52:47 AM
Hi,

 ok, I'll try on windows side when I have a chance

bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: FractalCore on October 15, 2013, 04:31:44 PM
Have you had a chance to try this on Windows yet. It would be interesting if it didn't work for you too.
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 16, 2013, 02:45:57 AM
Hi,

 Yes, I just tried, and indeed in editor, it's weird! but when I publish it's all fine and works as expected... I also tested without PlayMaker being involved and it's the same...

Bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on October 16, 2013, 03:00:31 AM
Hi,

 Ok, I found a way to solved this. I use Input.AnyKey instead of Input.AnyKeyDown and it works now everywhere including windows editor.

Please find attached the update version. I am not overwriting cause the behavior is slighlty different, you will get repeated input if you keep the key pressed, which is logical in a way.
Title: Re: Get Any Key Store Result as String
Post by: MABManZ on October 21, 2013, 04:12:30 PM
Thanks so much Jean it looks like everything is good!
Title: Re: Get Any Key Store Result as String
Post by: joduffy on April 26, 2014, 07:00:44 AM
Hi guys,

I am testing this on a windows 7 machine 64 bit and getting nothing stored into the string. It doesnt store when i click the mouse button or keyboard keys.

Is there something I am doing wrong
Title: Re: Get Any Key Store Result as String
Post by: joduffy on April 26, 2014, 07:24:05 AM
I did find this:

http://issuetracker.unity3d.com/issues/input-dot-inputstring-works-differently-on-windows-and-on-mac

The other option is to try something like this:

Event.isKey

bool isKey;
Description
Is this event a keyboard event? (Read Only)

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {
    void OnGUI() {
        Event e = Event.current;
        if (e.isKey)
            Debug.Log("Detected a keyboard event!");
       
    }
}

But this belongs in the OnGUI. Not sure how to access it from a custom playmaker script.

http://docs.unity3d.com/Documentation/ScriptReference/Event.html
Title: Re: Get Any Key Store Result as String
Post by: joduffy on April 29, 2014, 01:53:35 AM
I wrote a C# to store the results of a key press. This excludes mouse events/buttons/actions.

You access the results via a get property action in a FSM and store it in a FSMString.

You can also use the script to trigger an event in a playmaker FSM by giving the name of the event to send and also the FSM in which the event lives. This is done in the inspector panel.

I have attached the script and also displayed the code below.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Input)]
[Tooltip("Sends an Event when a Key is released.")]
public class GetKeyUpFromString : FsmStateAction
{
[RequiredField]
public FsmString key;
public FsmEvent sendEvent;
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

public override void Reset()
{
sendEvent = null;
//key.Value = KeyCode.None;
storeResult = null;
}

public override void OnUpdate()
{
bool keyUp = Input.GetKeyUp(key.Value);

if (keyUp)
Fsm.Event(sendEvent);

storeResult.Value = keyUp;
}
}
}
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on June 03, 2014, 08:08:09 AM
Hi,

 Is that action working on windows phones then?

Bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: LogLady on January 16, 2015, 12:31:16 PM
I used the example that jeanfabre made on this topic but the backspace is not working. It is not deleting the last typed character. Is there a way to make it work?
Title: Re: Get Any Key Store Result as String
Post by: jeanfabre on January 19, 2015, 01:40:59 AM
Hi,

 Are you using the new UI system? I think fields will work a LOT better then with other solutions.  Catching keys manually seems odd on windows unfortunatly.

 Bye,

 Jean
Title: Re: Get Any Key Store Result as String
Post by: LogLady on January 20, 2015, 10:25:57 AM
Maybe that is the problem. I'm using windows 7 and the pressed keys (as said before on this topic) only blinks. I hope that the Unity guys can solve it sometime...