Author Topic: Press-and-Release?  (Read 4490 times)

Gamer Dad

  • Guest
Press-and-Release?
« on: May 13, 2019, 08:31:08 AM »
I'm playing a game where different commands are executed depending on how long a key is pressed.  For example, a 1-second (short) press of the E key may do one thing while a 3-second (long) press of the E key may eject the pilot or quit the game.

Then I need to tie that command to a hat switch in my joystick.  Basically, I need the key to be pressed for however long I keep the hat switch held in a particular direction (UP, for example).  So while the hat switch is held UP, the E key is pressed down.  When I release the hat switch, the E key is released.

Any help on how to get this done or which section of the manual to read up on would be much appreciated!  Thanks!

iceblast

  • Sr. Member
  • ****
  • Posts: 372
Re: Press-and-Release?
« Reply #1 on: May 13, 2019, 02:32:53 PM »
Here's the command I use. Works very well.

Tap the key, and it does something, hold the key 0.3 seconds, and it does something else.

The 3 is actually 0.3 seconds, if you need it to be 3 seconds, change it to 30.

Code: [Select]
Set small int (condition) [Seconds] value to 0
Start Loop While :  Keyboard Key 'F14' Is Pressed
    Set small int (condition) [Seconds] value as incremented by 1
    Write [Blue] '{SMALL:Seconds}' to log
    Pause 0.1 seconds
    Begin Small Integer Compare : [Seconds] Is Greater Than 3
    End Condition - Exit when condition met
End Loop
Begin Small Integer Compare : [Seconds] Is Less Than 3
End Condition - Exit when condition met


Gamer Dad

  • Guest
Re: Press-and-Release?
« Reply #2 on: May 13, 2019, 03:56:08 PM »
Thanks but how do I tie that to a joystick hat switch please?

iceblast

  • Sr. Member
  • ****
  • Posts: 372
Re: Press-and-Release?
« Reply #3 on: May 13, 2019, 05:23:44 PM »
If you assign the joystick to VA, and set the hat switches, you should be able to trigger the command with the hat switch direction you assign to it.

iceblast

  • Sr. Member
  • ****
  • Posts: 372
Re: Press-and-Release?
« Reply #4 on: May 13, 2019, 05:28:20 PM »
Hang on, I think I read what you wanted to do wrong.

Code: [Select]
Start Loop While :  Keyboard Key 'E' Is Pressed
Press down E key
End Loop
Release E key

That command will hold E down, till you release it.

Gamer Dad

  • Guest
Re: Press-and-Release?
« Reply #5 on: May 13, 2019, 06:10:01 PM »
I think there's a little bit of misunderstanding here.  This is how I want it to work:
  • Move HAT switch to UP position and hold
  • Press down E key
  • Release HAT switch
  • Release E key


So far, here is the code that seems to work:
Quote
Start Loop While :  Joystick 1 Button 15 Is Pressed
    Press down E key
End Loop
Release E key

However, you will notice that it is Joystick 1 Button 15.  When selecting Device State, I can only select joystick buttons (up to 128) and not Joystick 1 POV 1.  There is no option to select the joystick POV.

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2824
Re: Press-and-Release?
« Reply #6 on: May 13, 2019, 06:26:34 PM »
First, you have to tell VA to treat its detection of your POV positions as buttons.  You'll find this on the joystick options page.  You'll have to determine which pov is being used and select the proper radio button.

The simplest thing you can do once you get that set up is make two commands - one that gets triggered when your POV is pressed that presses your E key down and a second that gets triggered on the same POV position, but only when it is released (you select the option to execute only on release) - that command would release your E key.

I've attached a sample profile that does just this (with joystick 1 POV 1 as the sample).


You can do all this in one command, but it involves looping and checking device states.  The two-command sample seems to work pretty well.



Edit - I do believe that the POV positions become the upper end of the 128 buttons that can be assigned (I'd have to dig in to see if that's the case).

Gamer Dad

  • Guest
Re: Press-and-Release?
« Reply #7 on: May 13, 2019, 06:46:59 PM »
First, you have to tell VA to treat its detection of your POV positions as buttons.  You'll find this on the joystick options page.  You'll have to determine which pov is being used and select the proper radio button.

I think I've done that already?




Your attached profile works and I think that is what I need, will have to check!

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2824
Re: Press-and-Release?
« Reply #8 on: May 13, 2019, 08:14:17 PM »
You'll just need to adjust it to whatever POV button you are using and that should do it ;)

Gamer Dad

  • Guest
Re: Press-and-Release?
« Reply #9 on: May 14, 2019, 07:05:15 AM »
It's a 8-way hat but I just want to use 4 directions so chose that one.

Your solution works excellently!  I downloaded a pre-made profile and the solution looks out of place in stark contrast to the tidyness of the rest of the profile but if it works, who am I to be picky?!  8)

Acecool

  • Newbie
  • *
  • Posts: 5
Re: Press-and-Release?
« Reply #10 on: June 08, 2019, 03:39:51 AM »
You don't need to keep sending the command to press the E key...

The logic should look something like...

Code: [Select]
//
loop while joystick hat key pressed..
    if pressed is not set, or is set to false
        set bool pressed to true
        press key e down
    end

    pause 0.1 seconds
end

release key e
set bool pressed to false


What happens then, is the e key is pressed and held the first iteration of the loop, every other iteration nothing happens other than the sleep - a controlled toggle ( the variable, ensures the key is only activated once ). The loop checks for updates every 0.1 seconds ( you can change this ). When the loop is exited, when the joystick hat switch is released, then the key e is released and the variable is reset.

Pseudo code, but the logic is sound.

You can use these types of controlled-toggles for other things too. There can be a downside though. If you press the e key on your keyboard - it won't trigger E again in the loop so if there is a way to see if E is pressed then that would be the better method instead of the variable... So if e is pressed and released and resets to state, then it would be re-pressed.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Press-and-Release?
« Reply #11 on: June 08, 2019, 02:38:33 PM »
If the command triggers off your joystick hat, you can send the press before the idle loop...

E.G.
Code: [Select]
Press down E key
Start Loop While :  Joystick 1 POV 1 Button 1 Is Pressed
End Loop
Release E key