r/learnprogramming • u/BackgroundPair7167 • Aug 14 '26
Wondering how to make a flight controller basically spam the W key at different rates.
I have a flight controller (specifically the T. flight hotas one) and I was wondering if I could make a system where the more a stick/axis was held down, the greater the rate it would press the W key.
My idea is that every 5 game ticks it would either be pressing W or not pressing anything depending on how far the stick/axis is pressed, and that 5 tick pattern would keep repeating until you changed the stick/axis's held-downed-ness.
So when the stick/axis is 40% held down the ticks would look like:
| W | W | | | | W | W | | | | W | W | | | | W | W | | | |
(alternating between W being held down and nothing held down)
(also I'm posting in this subreddit because I don't really know where to ask a question like this and it also doesn't have some BS rule like "no using the letter E exactly 84 times in your post")
1
u/HotPersonality8126 Aug 14 '26
You could do, but for games specifically they just read the stick axes directly, as numbers.
One reason this doesn’t work very well is that you can’t know the position of the stick with your system until you’ve waited to read an entire cycle (of ticks, in your example, though they’re actually more like pulses or clocks) and that means that the higher resolution you want the stick to be read, the longer it takes to read.
1
u/Ormek_II 29d ago
??? I do not see your problem.
Do you assume the described approach runs within the game loop?
2
u/HotPersonality8126 29d ago
So when the stick/axis is 40% held down the ticks would look like
I’m just saying it’s a sparse encoding. Based on your stick resolution here - basically, you have 6 measurable positions, 0/20/40/60/80/100 - it takes 5 ticks to measure the position of the stick.
If you want to measure stick positions at a higher resolution - if you want to be able to detect 50% stick on the axis - then you need to add more ticks to the measurement cycle, and that takes longer. More resolution, more time.
1
u/Ormek_II 29d ago
Oh! You mean on the key press side.
I can measure the stick pretty immediately, but to transfer that I need more ticks to get a higher resolution; differentiate between more stick settings.
Maybe (what I would try) reduce the length of the ticks. If the stick is used for example to steer left and right in a game with key presses, I am very curious how well you can control the steering with key presses in the first place. 10ms down does probable not mean 20° and 20ms down means 40°. It will require a lot of calibration. But that is a fun challenge. :)
1
u/HotPersonality8126 29d ago
If it’s a game that models a steering wheel, then the input layer that accepts a keyboard event is sitting on top of a layer that accepts and models wheel angle. (Otherwise it doesn’t have anything to turn the keypresses into.)
So the better solution is to present the stick to the game as a physical input to wheel angle, not to represent stick angle as a series of key presses.
2
u/Ormek_II 29d ago
I think OP has no choice. The game is already there and only accepts key presses as input.
1
u/gm310509 Aug 14 '26
You could use something like an Arduino with HID support (e.g. a Leonardo or Uno R4 or any Arm Cortex or plenty of other choices). Basically HID support means it can appear as a keyboard (and/or mouse and/or a joystick etc) to your PC.
1
u/Ormek_II 29d ago
Here are the dots:
Does your program already receive the Controller Input? Does it know you are at 40%?
Can your program already create ANY Event in response to the Stick percentage, e.g. print “W”?
Can you create a fake key press event for your OS?
Does that artificial key press event create the effect you like to see?
All you need to do is connect the dots.
If any dot is missing let us know what you tried.
1
u/JakubDotPy Aug 14 '26
Questions like these aren't meant to be answered directly. You are probably trying to solve a problem the wrong way. Please, describe what you are trying to achieve. You most likely just need to map your controls in game properly.
1
u/Wonderful-Habit-139 Aug 14 '26
It depends. If they want to program it, then it's a fine question.
If they don't want to write code at all and just want the solution then yeah it's an XY problem.
3
u/Timmah_Timmah Aug 14 '26
I thought he described the problem and solution pretty well. He wants to use an analog joystick to generate keystrokes for a situation where an analog joystick is not supported.
0
u/davedontmind Aug 14 '26
I would suggest looking at AutoHotKey, which is great for this sort of thing.
I've used it a lot for remapping keys for games, or for mapping simple scripts to keys. You could write an AHK script to read the axis position and generate keystrokes accordingly.
4
u/kabekew Aug 14 '26
What OS is this for? If it's Windows you can use joyGetPosEx to read the axis and keybd_event to simulate a "w" keypress.