i have written this code to pilot a cursor on a radar from 2 axis (simulation combat plane A10-C DCS for example)
so 4 keys to move cursor Up, Down, Left and right and the possibility to have an Xtra keys for all moving key (Lshift or other but the same key for all)
Code: Select all
import thread, time
class var:
up, dn, lt, rt = 0, 0, 0, 0
kUp, kDown, kLeft, kRight, kXtra = None, None, None, None, None
if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 30
ky = var()
slewrate = 0.05
def SlewXY(x, y):
slewing = ky.up or ky.dn or ky.lt or ky.rt
ky.up, ky.dn, ky.lt, ky.rt = int(y < 0), int(y > 0), int(x < 0), int(x > 0)
if (not slewing) and (ky.up or ky.dn or ky.lt or ky.rt):
thread.start_new_thread(Slewkey, (0.05,))
def Slewkey(slewrate = 0.05):
while True:
if not(ky.up or ky.dn or ky.lt or ky.rt):
break
down = True
for i in range(2):
if i:time.sleep(slewrate)
if ky.kXtra is not None:keyboard.setKey(ky.kXtra, down)
if ky.up:keyboard.setKey(ky.kUp, down)
if ky.dn:keyboard.setKey(ky.kDown, down)
if ky.lt:keyboard.setKey(ky.kLeft, down)
if ky.rt:keyboard.setKey(ky.kRight, down)
down = not down
if ky.up or ky.dn or ky.lt or ky.rt:
time.sleep(0.11 - slewrate)
def KeyToCurXY(x, y, range, fct, keyUp, keyDown, keyLeft, keyRight, keyXtra = None):
slewing = ky.up or ky.dn or ky.lt or ky.rt
ky.kUp, ky.kDown, ky.kLeft, ky.kRight, ky.kXtra = keyUp, keyDown, keyLeft, keyRight, keyXtra
NbrZone = len(range) - 1; x1 = None; y1 = None
for i, r in enumerate(range):
z = NbrZone - i - 1 # z is the zone of stick value z= 0, 1 or 2
if x >= range[z] and x1 is None:
x1 = z - 1 # x1 = -1, 0 or 1
if y >= range[z] and y1 is None:
y1 = z - 1 # y1 = -1, 0 or 1
if x1 is not None and y1 is not None:
x, y = x1, y1; eval(fct) # eval("SlewXY(x, y)") -> call the SlewXY function
break
KeyToCurXY(x = joystick[1].x, y = joystick[1].y,
range = [0, 10, 90, 100], fct = "SlewXY(x, y)", # x and y = -1, 0 or 1 following the zone of the stick values
keyUp = Key.U, keyDown = Key.D, keyLeft = Key.L, keyRight = Key.R,
keyXtra = Key.LeftShift )
if stopping:
diagnostics.debug("End of script")
ky.up, ky.dn, ky.lt, ky.rt = 0, 0, 0, 0 # in case of you stop Freepie to be sure the thread stops
in this sample, the values of x and y are in range [0, 100] so
i have defined 3 zones: [0, 10] , [ 10, 90] and [90,100] (-1 , 0 or 1)
following the value of axis x (and y) i send the up, down or left, right key to move the cursor
in the middle zone = no move (dead zone)
you could adjust the slewrate (0.05 = 50% is a good value) slewrate = 0 to 0.1 (0 to 100%)
you could see the key typed in notepad