These are the Support Forums for FSUIPC and related products.

These forums replace the old support forums which have been running for 20+ years. The old forums contain a wealth of information on all FSUIPC products, but are now no longer available.

Please note that you will need to Register and Login to post for support, and also to download attachments. You can view these forums without registering.

Support is also available via Discord. Please use the following invite link to join the FSUIPC Discord server:
https://discord.gg/zjwUTrxmmE

Assigning a Key Pre...
 
Notifications
Clear all

[Solved] Assigning a Key Press to a Lua Script

Posts: 3
Registered
Topic starter
(@monterey56)
Joined: 4 weeks ago

Hello John and Peter,

I am trying to assign the Alt+H key stroke into a lua script.  My Alt+H is assign in FS2024 Controls Keyboard to do two things,

The lua script will monitor the airplanes bank angle and if it exceeds 5 degrees the lua script will not fire off the ALT+H command. So basically it will keep everything sync-ed while in level flight.  I is inhibited on the ground.  I can manually press the Alt+H key and it works, but I have not been successful getting the lua script to do this function for me.

Using AI for help, and they do a great job most of the time, but everything we tried so far is not working.  I am not asking you to write a lua script for me, but only asking for advice on, is there something we are missing here?

Thanks in advance for any tips to get this to work,

Ed


6 Replies
Posts: 265
Moderator
(@fsuipc-support)
Joined: 7 months ago

In MSFS2024, if a key (combination) is assigned in MSFS, it will not be seen by FSUIPC and therefore it will not start your lua script if it is also assigned in FSUIPC to do this. You need to remove the assignments in MSFS2024. Note this seems to be new behavior since the latest MSFS release, as previously (and in MSFS2020) keys assigned in MSFS itself were also seen by external apps via the SimConnect API.

But even if FSUIPC received this key press combination and started your lua script. you would not be able to prevent the key assignments in MSFS to the same keys from being triggered. Therefore it is better to remove the assignments in MSFS, assign in FSUIPC and send the required controls (or not) in your lua script.

To know what is being seen by FSUIPC, you can activate logging for Buttons & Keys and open the FSUIPC logging console/window, which will then log all key presses/releases that MSFS sends to FSUIPC.

(You posted the same message again under a slightly different topic name - I have deleted this).

 


Reply
Posts: 265
Moderator
(@fsuipc-support)
Joined: 7 months ago

Also, it would be more efficient to have a lua script already running (i.e. auto-started - see the documentation for details on how to do this) and use the event.key lua function to intercept the key press and have your logic and code to send the required controls in the event handling function.


Reply
Posts: 3
Registered
Topic starter
(@monterey56)
Joined: 4 weeks ago

Thank for the reply and this was a tough one for me as a new student of your powerful program FSUIPC.  I have had it for decades. but never really understood it too much until I started using AI and what an education I am getting.  It is absolutely revolutionary.

We finally figured it out and the key was to use offsets and the assignment/buttons and switches, select a unassigned button on your controller, then check select for FS control and search the dropdown window for something that looks close to what your key stroke does, in my case as stated above in my first post.  I found the function Heading Gyro Set and tested it with the newly assigned button, and to my surprise it did both functions as were assigned in Settings/Controls/Keyboard that were assigned to my ALT+H keyboard key. Then the button assignment was deleted as we now knew what K: variable to use.

Now with this infomation the lua script was written.

-- SetHdgBug.lua
-- Automatically syncs heading bug and gyro every 20 seconds
-- Inhibited if on ground or bank angle exceeds 5 degrees
-- FSUIPC7 / FS2024 / Redwing L1049G TWA
-- Solution: Uses FS2024 Input Event HEADING_GYRO_SET
-- which syncs both gyro and heading bug in one command

while true do
    ipc.sleep(20000)
    local onGround = ipc.readUB(0x0366)
    local bankRaw = ipc.readSD(0x2B54)
    local bankDegrees = bankRaw * 360.0 / (65536.0 * 65536.0)
    if onGround == 0 and math.abs(bankDegrees) <= 5.0 then
        ipc.log("SetHdgBug: FIRING")
        ipc.execCalcCode("(>K:HEADING_GYRO_SET)")
    end
end

It was tested and it was functional and after about 10 hours of trail and error the gyro and heading bug will be reset for drift.  I should say that this is for the new Redwing L1049 Connie which is also using scoped variables which is another subject all in itself.

So to further my understanding of this, all those items in the drop down window are all the assignments that are found in Setting/Controls/Keyboard?  My lua scripts are there too of course and then you can write your lua script using offsets to control what the script will do, value 0,1,2,3,4,5 etc.

Example offset 0x0366 the value of 0=the airplane is in the air, the value of 1=On Ground.

This has been a very exciting week for me am I've written over 25 lua scripts that control so many panel functions in the Connie, so many that I have competely run out of controller buttons, this happens very quickly.

So in the mail here to the USA are two Leo Bodner BBI-32 Button Box Interface - With Connectors boards and I am going to build my first button box using FSUIPC and lua scripts to assign the switches, buttons, knobs, etc.

Thanks again for the reply, sorry about the double post and if you have any other tidbits of information that you could pass along to me that would be great, but I know you are extremely busy.  AI is a great tool for anyone trying to learn FSUIPC.  I am going to be doing more testing with the concept stated above.  I do have the [Auto] defined in my FSUIPC.ini file.

Have a great day,

Ed


Reply
Posts: 265
Moderator
(@fsuipc-support)
Joined: 7 months ago

Glad you got it working. I have taken the liberty to format your last post - when you add lua code (or any other code), you should use the code formatting button ('<>') so that it appears correctly - otherwise it tends to be unreadable!

A few minor points:

Posted by: @monterey56

Solution: Uses FS2024 Input Event HEADING_GYRO_SET

Be careful of terminology: HEADING_GYRO_SET is not an Inout Event, it is a key event, or standard FS control.
The term 'Input Event; should only be used to describe Input Events, which in calc. code are referred to as 'B' type events/variables. You are using a K-type variable, which is a standard control with number 65568. So rather than:

Posted by: @monterey56

ipc.execCalcCode("(>K:HEADING_GYRO_SET)")

you could also use

    ipc.control(65568)

Posted by: @monterey56

select for FS control and search the dropdown window for something that looks close to what your key stroke does, in my case as stated above in my first post.  I found the function Heading Gyro Set and tested it with the newly assigned button, and to my surprise it did both functions as were assigned in Settings/Controls/Keyboard that were assigned to my ALT+H keyboard key.

That is fine, but be aware that many aircraft in MSFS2020/2024 do not use the standard controls, or they may use them partially, i.e. they may control the function but maybe not the visuals. There are many other methods of control in MSFS, including Input Events (aka B-vars), Lvars, Hvars, and more complex calculator code (which can be used for ALL variable/event types) usually wrapped-up as a 'preset'. To implement many functions, it is generally a good idea to first see if there is an available preset (see https://hubhop.mobiflight.com/presets/) and look at the calc code the preset uses to determine how the switch/knob/etc is controlled. You can use the preset directly, but if the preset is simple and just sends a K-event, or updates an lvar, sets a h-var, or calls a B-var, you could assign to these directly if you prefer, but it is always at least worth checking for a preset first.

Posted by: @monterey56

which is also using scoped variables which is another subject all in itself.

Yes, these new scoped lvars are a bit of a pain, and the only way to use them at the moment is via calculator code. Reading a value from a scoped lvar is even more difficult, as you need to copy the value to a standard lvar first to be able to do this (using FSUIPC).

Posted by: @monterey56

So to further my understanding of this, all those items in the drop down window are all the assignments that are found in Setting/Controls/Keyboard? 

Not quite, although they should be. What you see, when selecting for FS Controls, are the events/controls/K-variables that are exposed externally by the MSFS SDK. Unfortunately, some of these will not work (e.g. view and slew controls), and there are also some others that you can assign to in  MSFS but which are not exposed via the SDK, and so cannot be used. If you check your FSUIPC Documents folder, there is a text file there that lists all the available FS controls/events, in both numerical and alphabetical order.

Cheers,

John

 

 


Reply
Page 1 / 2