From The Depths - Forum

Full Version: Detect key pressed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
My Lua scripts have evolved well, but now i find my self needing to give user input to them. Here are some examples.

Key to detonate already launched missiles if target is standing down (to allow capture).
Keys to change desired altitude on my air ship. I have a script that controls all my thrusters and maintains altitude relative to water or terrain, i want a key to change that value.

So, how do i detect that a key is pressed?
Please only LUA related solutions.
I haven't dug into 2.0 much yet, but I don't believe there's a direct way to get keypresses. However, there are a number of roundabout ways to go about creating a "switch" you can trigger with a key. The component API has access to a number of devices, which Lua can both read from/write to: balloons, pumps, tractor beams, etc. Look at the Component_* methods, specifically.

Set up a complex controller, bind keys to the device, read the state from Lua. Since you access most things in Lua via their index, distinguishing between which ones are your control and which ones aren't is left as an exercise. Wink (I typically use the direction that the block is facing...)

As for manual altitude control, I think the best option is the drive maintainer. Because:
  1. It's analog and lets you go smoothly from -1 to 1
  2. You can reset it to 0 by hitting your increase/decrease keys simultaneously (just like throttle on water craft)
  3. You can display the current value on the HUD by enabling it in options (primary, secondary, tertiary drive).
Pretty much all of my aircraft & submarine scripts allow optional manual control via drive maintainer. It's really nice.
Reference URL's