Change of Command Key to Joystick

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Change of Command Key to Joystick

20 Feb 2019, 06:46

I am an FSX flight simmer and have recently started to use a very interesting program called VAC (Voice Activated Commands) which makes it possible for you to carry out voice commands for the Flight Sim commands. These commands are relayed to FSX by pressing the MButton of the mouse and holding it until you have spoken the command. This works perfectly.

Now what I am trying to do is transfer from using the MButton on the mouse to using one of my buttons on the yoke. I believe that this can be done with the aid of a small AHK program. Now this is where I am having the problem due to my limited experience with AHK. I have been using AHK for years mainly for menu presentation, file organising etc. but I am afraid after a lot of trial and error I have failed to program the above.

I would be most grateful if someone could help me with this.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

20 Feb 2019, 07:27

You cannot alter how windows sees a physical joystick - it is impossible programatically to press a button on your actual yoke.

Also, AHK does not support pressing of joystick buttons natively, you need to use a 3rd-party driver such as vJoy, plus an AHK library to drive it.
See CvJoyInterface for such a library.
So when doing this, you have two options:
1) Read all the inputs from your yoke, and pipe them through to the vJoy stick, then map the game flight controls to the vJoy stick
2) (Much simpler) Output ONLY the stuff you wish to remap to the vJoy stick, and map ONLY those game functions to the vJoy stick.
Obviously the game will need to support taking input from multiple sticks, but I guess FSX would fall into that category.

It's also worth noting that you don't really need to be writing AHK scripts to do this.
See UCR-AHK (Link in signature) or UCR-C# for a GUI app that will do this for you.

You also may be interested in this AHK library: HotVoice.
This adds VAC-like functionality to AHK. You can have AHK code directly respond to voice commands, and send the keys itself, without needing to use an intermediary
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

20 Feb 2019, 09:51

My sincere thanks for your prompt reply.

I will examine all of your help and go through it thoroughly.

In the meantime just to let you know why I followed the line of using AHK to accomplish what I explained. I went through all the information available from the home site of VAC and from that was advised that VAC was compatible with AHK and that several members had used AHK to provide the necessary acceptable link. It was also explained that the way it was done was through AHK to get the MButton of the mouse to be accepted as a keyboard character , such as "F12" or "CTRL+SHIFT+R" or any such character or group of same that was available.
Now I searched and searched and eventually found a AHK program that someone had used which did not perform exactly the function which I wanted but was very much on the same lines. Now I copied this program and made small changes which I thought were acceptable and tried it. It was accepted by AHK as a program but did not transfer the command.
I outline below My Effort to Program and also the Original which I mention above .Perhaps you would have a look at both below and see if anything could be used on the same lines ?

MY PROGRAM

MButton::F12
#IfWinActive
#IfWinActive ahk_class ArmA 2 OA
MButton::
Send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}
return

1Joy1::
send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}

ORIGINAL
XButton2::F13
#IfWinActive ahk_class ArmA 2 OA

XButton2::
Send {XButton2 down}
VA_SetVolume(10)
Keywait, %A_ThisHotKey%
Send {XButton2 up}
VA_SetVolume(100)
return

1Joy7::
send {XButton2 down}
VA_SetVolume(10)
Keywait, %A_ThisHotKey%
Send {XButton2 up}
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

20 Feb 2019, 11:23

Oh, hang on, upon re-reading of your post, I think I maybe got things the wrong way around.
You want it so that when you hold a button on the joystick, it presses MButton on the keyboard?

I see a number of issues with your script...

1) There are two MButton hotkeys:

Code: Select all

MButton::F12
and

Code: Select all

MButton::
Send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}
return
Delete BOTH of them. These hotkeys control what happen when you hit MButton on your mouse, which is not what you want.

2) Badly coded hotkeys
This is not the way to do a pass-through hotkey:

Code: Select all

XButton2::
Send {XButton2 down}
VA_SetVolume(10)
Keywait, %A_ThisHotKey%
Send {XButton2 up}
VA_SetVolume(100)
return
Do this (Add ~ prefix to hotkey):

Code: Select all

~XButton2::          ; ~ means pass-through. Will not block original function of key
VA_SetVolume(10)
Keywait, %A_ThisHotKey%
VA_SetVolume(100)
return
3) Some of your hotkeys are missing the return statement at the end

Code: Select all

1Joy1::
send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}
This should have a return statement after it.

4) Your press / release hotkeys are coded in such a way as they will potentially conflict with each other.

Code: Select all

1Joy1::
send {MButton down}
Keywait, %A_ThisHotKey% ; <--- CAUSES ISSUE!
Send {MButton up}
return

1Joy7::
send {XButton2 down}
VA_SetVolume(10)
Keywait, %A_ThisHotKey% ; <--- CAUSES ISSUE!
Send {XButton2 up}
return

XButton2::
Send {XButton2 down}
VA_SetVolume(10)
Keywait, %A_ThisHotKey%
Send {XButton2 up}
VA_SetVolume(100)
return
If you hold button 1, then hold button 7, then release button 1, THE CODE THAT IS MEANT TO RUN WHEN YOU RELEASE BUTTON 1 WILL NOT RUN UNTIL YOU ALSO RELEASE BUTTON 7!!
This is due to the way AHK threading works.
For keyboard and mouse keys, this is a simple fix - simply use an up event hotkey:

Code: Select all

XButton2::
Send {XButton2 down}
VA_SetVolume(10)
return

XButton2 up::
Send {XButton2 up}
VA_SetVolume(100)
return
For joystick buttons, however, this is not so simple, due to a bug in AHK that will never be fixed (Up event hotkeys for joystick buttons fire on press of button, not on release).
In this case, you need to use SetTimer:

Code: Select all

1Joy1::
send {MButton down}
SetTimer, WatchB1, 10
return

WatchB1:
if (GetKeyState("1Joy1"))
   return
SetTimer, % A_ThisLabel, Off
Send {MButton up}
return
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

20 Feb 2019, 12:27

Also, FYI, I dunno if you use all the functions of VAC (eg speaking back what you just did), but there are some things that my HotVoice library can do that VAC cannot, which you may find useful:

Send joystick output (Using the linked library)
Compound commands - with VAC, it seems like if you wanted a command like "Set Flaps to 50%", you would need to define a command for every possible number (eg Flaps 1%, Flaps 2% etc). With AHI, you can define a command "Set Flaps to <value>" and then it fires an AHK SetFlaps(value) function and it passes it whatever number you spoke. Combined with the aforementioned output to joystick, and this could be very useful, as you could have an axis in DCS specially dedicated to flap angle, and have the script control it.
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

20 Feb 2019, 14:42

Again my thanks for your help.

I am afraid that we have a misunderstanding in the reading of my last mail. I outlined what was my attempt at a program and showed it as MY PROGRAM
I also showed the program which I found on site that I modeled my program on and I called this ORIGINAL in my mail.

Now from what I can see you have run the two programs together and understood that they were all part of the one program. Please ignore the program marked ORIGINAL as I only put that there to indicate how I arrived at my attempt to write the AHK program.

My program is solely the following ;

MButton::F12
#IfWinActive
#IfWinActive ahk_class ArmA 2 OA
MButton::
Send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}
return

1Joy1::
send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}

Please IGNORE anything else .(The second program ,called ORIGINAL,which I showed was written by someone that was trying to lower the volume in his program. This is nothing to do with what I am
trying to do.)

In your Mail you have explained a lot where I went wrong which I can understand, however, your explanation treated the two programs as one which makes it a little difficult for me to get a clear picture of how to re-write my own program. Could I be so bold as to ask for help on this ?

I accept full responsibility for not making it more clear regarding the two programs I included in my first mail and am very sorry about that.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

20 Feb 2019, 16:13

If the whole program only needs to send MButton when you press 1Joy1, then this code is all you need:

Code: Select all

#IfWinActive ahk_class ArmA 2 OA
1Joy1::
send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton up}
return
If this does not work, then most likely your problem is that you need to run the AHK script as admin.

If you need to add more joystick mappings (eg you add a 1Joy2:: hotkey and try to use the same code), then you will run into the problems I mentioned before.

BTW, to do this, you do not need to write AHK scripts. You can use an application called UCR - see the link below in my signature.
Add a "Button To Button" plugin, map the input to the stick button, map the output to MButton
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

21 Feb 2019, 16:00

Again many thanks for your help.

I am afraid I do not have any good news regarding getting AHK to accept and run a program. I will outline for you what I have tried without success.

1. I used your last suggestion and also tried every possible change of same but I am afraid no go.It was accepted as a program but would not provide the link to the mouse to carry out commands.

2. I also ran the AHK script as administrator with no luck.

3. You mentioned that it was not necessary to write an AHK script as the same could be accomplished by using your UCR program. I will explain to you why I left this option until last. I have been a cockpit builder down through the years and am using five gamepads, seven monitors and two PC's in a network in my set up. Over the years AHK has been invaluable to me and I have been using it to provide Menu's, Sub Menu's, Flight Selections, Placement of Files etc.etc. I was anxious to include the above in one of the AHK programs that I use without using a further program. All I was looking for was ONE button to button connection which I thought would be easy to achieve. One should never underestimate what is required ! ! .

4. I have downloaded your UCR and have installed same. It is an excellent program and "Does what it says on the tin" to coin a phrase. It is unfortunate that I only require it for the one function above. I have it prog. to run auto at startup. Just one thing I wanted to ask you regarding same, I notice that any new profiles that you arrange with it are loaded in the UCR.ini file and wondered was there any way ( as I have created and use only one profile ) that the main program is loaded up all the time with the facility to create more ? Probably a silly question.
My sincere thanks to you for all the help you have given me and for which I appreciate very much.

Toni
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

21 Feb 2019, 17:35

Well if UCR did what you wanted, then it is possible with AHK, so we just have to work out why your stand-alone script is not working. As you say, it's overkill to use a big app just for one mapping.

Before I continue, I just want to clear something up that I am unsure of. You said "These commands are relayed to FSX by pressing the MButton of the mouse and holding it until you have spoken the command". I am assuming that by this you mean that VAC is watching MButton, and when it sees it held, it starts listening for voice commands.

So we should isolate the problem. We need to work out which part of the chain is not working.
First, we need to find out if the AHK script is seeing the joystick OK.
This is as simple as running this script:

Code: Select all

1Joy1::
   SoundBeep
   return
You should hear a beep when you hit the button.

Make sure it works while in-game also.

If that works, then we need to see if VAC is capable of seeing keys sent by AHK.
You don't even need to use joystick for this, you could just use something like F12::MButton to remap F12 to MButton - can you activate VAC using F12 now?
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

22 Feb 2019, 07:38

Many thanks for your continued interest in my little problem. I would really get great satisfaction if it can be overcome.

On your question regarding and I quote ; "These commands are relayed to FSX by pressing the MButton of the mouse and holding it until you have spoken the command". I am assuming that by this you mean that VAC is watching MButton, and when it sees it held, it starts listening for voice commands.. You are perfectly correct in your interpetation.

Now the good news. I ran the script which you advised ;

1Joy1::
SoundBeep
return

And when I hit the button I got the expected BEEP

I then added the following to the script as you advised ;

F12::MButton

And when I pressed F12 I was in control of sending VAC commands in other words I was able to activate VAC using F12. My current script now reads the following ;

F12::MButton

1Joy1::
SoundBeep
return

Can I anticipate from this that we are nearly there ?

P.S. I have tried, through FSUIPC, to assign the Button to F12 but it will not accept it and gives a bleep sound with a NULL message ! ! !
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

22 Feb 2019, 08:11

Well if both of the parts worked in isolation, not really sure why putting it all together is not working.

Let's try something else - maybe it's the holding of MButton that is not working properly for some reason.
Maybe try this?

Code: Select all

1Joy1::
   Send {MButton down}
   return

1Joy2::
   Send {MButton Up}
   return
Button 1 should press MButton, button 2 should release it
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

22 Feb 2019, 12:26

Hi there

Well at last with thanks to all of your advice it has finally worked.

From your last set of directions I arrived with the following script which seems to have been what we were looking for. I do not know if there is anything else I should add to this for to put iceing on the cake but I am very happy with it. What do you think ? ;

1Joy1::
#IfWinActive ahk_class ArmA 2 OA
Send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton Up}
return

I am extremely grateful for the time you have afforded me in arriving at this solution and I can safely say that without your help it would not have been possible.

Again my sincere thanks

Toni
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Change of Command Key to Joystick

22 Feb 2019, 12:33

OK then, I think I know what your problem was.

In this code:

Code: Select all

1Joy1::
#IfWinActive ahk_class ArmA 2 OA
Send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton Up}
return
The line #IfWinActive ahk_class ArmA 2 OA has no effect at all, as it is inside the hotkey.
Before, it was in the correct place, before the hotkey, and was taking effect.

So my guess is that your #IfWinActive command is not targetting the correct window. What this line does is to restrict the hotkey to only working in a specified app. If you target the incorrect window, the hotkey will not fire at all
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

23 Feb 2019, 06:25

Hi there

Everything you say is perfectly correct. The line #IfWinActive ahk_class ArmA 2 OA should be outside the hotkey to have any effect, however, if you put it outside the script will not operate at all for as you say it does not refer to the correct window (or program) and it kills the script entirely. Now if you put it inside the hotkey it will contribute nothing to script but will allow the rest of what is in the hotkey to operate. That is why the script worked for me partially when it was inside.

As you remember from my earlier mail I picked up from the forum on the VAC site an AHK script which some person used to perform a control of volume in VAC when the commands were being passed through by pressing the mouse MButton. I thought that extracts from this AHK script could be used for my little project. This is where i have discovered that I went wrong.
The line above is obviously made up of three parts 1.#IfWinActive 2. ahk class 3. ArmA 2 OA. Now I have looked into what the third part stands for and it is actually the name of a GAME i.e. ArmA 2 OA and using this line in any other program or Game will not work as it refers specifically to this game. As ar as i could understand the line could be used if I remove the reference to this game and replace it with the correct window reference to FSX. I have tried putting in "FSX", "Microsoft", "FSX Flight Simulation", "VAC" and a few more (but I am only guessing), with no luck. By removing this line completely it will work but if you do not remove your pressing of the Button at an appropriate time it will run into a second command which you did not ask for.
I have found from trial and error that the most accepted time for the button to be pressed is three seconds.

Now I found if I remove the two lines ; #IfWinActive ahk_class ArmA 2 OA and Keywait, %A_ThisHotKey% and replace the whole script to read as follows ;

1Joy1::
Send {MButton down}
Sleep, 3000
Send {MButton Up}
return

This I know is very amateurish but it is the best I can come up with. What do you think ?
All advice and direction that you have given me has been excellent, correct and most helpful.

Toni
toni
Posts: 10
Joined: 20 Feb 2019, 06:11

Re: Change of Command Key to Joystick

23 Feb 2019, 08:04

[font=]Just a follow up to my last mail[/font]

You wont believe it after a lot of hit and miss's the following works perfectly ;

#IfWinActive Microsoft Flight Simulator X
1Joy1::
Send {MButton down}
Keywait, %A_ThisHotKey%
Send {MButton Up}
return

Again my sincere thanks for all your time and help

Toni

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww, Sarhad and 297 guests