Page 1 of 1
Mouse To Keyboard Script
Posted: Tue Dec 06, 2016 8:00 am
by Dylate
Hello.
I'm a disabled person, and I have only mouse. I want play some games but don't know programm languages, please help me.
Write this code, but don't work very well
Code: Select all
if starting:
flag = False
#---BUTTONS---
Up= Key.W
Down= Key.S
Left= Key.A
Right= Key.D
if mouse.wheelUp:
if flag:
flag= False
else:
flag= True
if mouse.wheelDown:
if flag:
flag= False
else:
flag= True
if flag:
if mouse.deltaX > 0 and mouse.deltaX > math.fabs(mouse.deltaY):
keyboard.setKeyDown(Right)
keyboard.setKeyUp (Left)
if mouse.deltaX < 0 and mouse.deltaX < -math.fabs(mouse.deltaY):
keyboard.setKeyDown(Left)
keyboard.setKeyUp (Right)
if mouse.deltaY > 0 and mouse.deltaY > math.fabs(mouse.deltaX):
keyboard.setKeyDown(Down)
keyboard.setKeyUp (Up)
if mouse.deltaY < 0 and mouse.deltaY < -math.fabs(mouse.deltaX):
keyboard.setKeyDown(Up)
keyboard.setKeyUp (Down)
if mouse.deltaX > 0 and mouse.deltaY > 0 and mouse.deltaX == mouse.deltaY:
keyboard.setKeyDown(Down)
keyboard.setKeyDown(Right)
if mouse.deltaX < 0 and mouse.deltaY > 0 and mouse.deltaX == -mouse.deltaY:
keyboard.setKeyDown(Left)
keyboard.setKeyDown(Down)
if mouse.deltaY < 0 and mouse.deltaX < 0 and mouse.deltaY == mouse.deltaX:
keyboard.setKeyDown(Up)
keyboard.setKeyDown(Left)
if mouse.deltaY < 0 and mouse.deltaX > 0 and -mouse.deltaY == mouse.deltaX:
keyboard.setKeyDown(Up)
keyboard.setKeyDown(Right)
if mouse.deltaY == 0:
keyboard.setKeyUp (Right)
keyboard.setKeyUp (Left)
keyboard.setKeyUp (Down)
keyboard.setKeyUp (Up)
I want script with delta cause with positions cursor move stops on window edges.
Can I write with FreePie some functianal like 8 direction 60 degree for each left-right-up-down and 30 degree for combine?
If yes how properly can write something like that?
Re: Mouse To Keyboard Script
Posted: Tue Dec 06, 2016 11:38 am
by flo2
I think this is possible. What I'd do is calculate the angle the mouse is heading based off delta_x and delta_y.
Code: Select all
angle = math.atan(delta_y/delta_x) #this is in radians
Then check if the angle is within a certain range for each key press.
I don't understand the 30 degree split between the 8 directions. Isn't it 45 degrees between keys presses?
http://i.imgur.com/eU9Fg1tl.jpg
Re: Mouse To Keyboard Script
Posted: Tue Dec 06, 2016 12:00 pm
by Dylate
60 degrees for left/right/up/down need me 'cause with 45 often triggers semi-direction.
Right now write this, works almost perfect, but again often triggers semi...
Code: Select all
def analogUpdates():
y = mouse.deltaY
x = mouse.deltaX
keyboard.setKey(analogUp, (y < 0))
keyboard.setKey(analogDown, (y > 0))
keyboard.setKey(analogRight, (x < 0))
keyboard.setKey(analogLeft, (x > 0))
if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 20
#---BUTTONS---
analogUp = Key.W
analogDown = Key.S
analogRight = Key.A
analogLeft = Key.D
button1 = Key.R
button2 = Key.T
button3 = Key.Y
button4 = Key.U
analogFlag = False
if mouse.wheelUp:
if analogFlag:
analogFlag = False
else:
analogFlag = True
if analogFlag:
analogUpdates()
if mouse.leftButton:
keyboard.setPressed(button1)
if mouse.rightButton:
keyboard.setPressed(button2)
And one more question, how I can assign more buttons for LBM & RBM like gestures
Code: Select all
if mouse.rightButton:
if mouse.deltaX>0:
keyboard.setPressed(button3)
if mouse.deltaX>0:
keyboard.setPressed(button4)
else:
keyboard.setPressed(button2)
Like this works, but not good.
How i can check button pressed for some period and trigger action with differents cursor position and don't losing click without holding?
Sorry for my English.
Re: Mouse To Keyboard Script
Posted: Wed Dec 07, 2016 3:14 am
by flo2
Code: Select all
def analogUpdates():
y = mouse.deltaY
x = mouse.deltaX
angle = math.atan(y/x) * 180 / math.pi #this is in degrees
#reset keys
keyboard.setKey(analogUp, False)
keyboard.setKey(analogDown, False)
keyboard.setKey(analogRight, False)
keyboard.setKey(analogLeft, False)
#assign key to direction (0 degrees is 3:00 on a clock, positive angles are counter-clockwise)
if(angle < 120 and angle > 60):
keyboard.setKey(analogUp, True)
if(angle > 240 and angle < 300):
keyboard.setKey(analogDown, True)
if(angle > -30 and angle < 30):
keyboard.setKey(analogRight, True)
if(angle > 150 and angle < 210):
keyboard.setKey(analogLeft, True)
I haven't tested it, but this implementation of analogUpdates should take care of the semi-directions.
This is a good example of how to use the Time module in python:
http://stackoverflow.com/questions/3620 ... ime-module
Don't forget to add "import time" at the top of the FreePIE (python) script.
Re: Mouse To Keyboard Script
Posted: Wed Dec 07, 2016 6:57 am
by Dylate
Divided by zero exception...
Thanks for tips about time, where I find info about threading and starting read book "Python for Noobs"
Your code burn my processor when I add stroke
if x==0
x=1
I can't play with this code only freepie eats 50% cpu...
But it's really nice example, maybe when I change processor I can play with this. Or maybe find another way calculate difference to find angle...
Re: Mouse To Keyboard Script
Posted: Wed Dec 07, 2016 12:37 pm
by zelmon64
Dylate wrote:I can't play with this code only freepie eats 50% cpu...
Hi. I thought I'd join in as this looks fun and I was surprised you said it takes so much cpu power. As I understand it, you want to be able to click-and-drag to activate keys or use the scroll wheel to start and stop the drag motion. Here is my attempt at this (it uses a max of 5% on my 3.5 GHz cpu). It has a small deadzone before mouse motion will initiate a key press and a gap of 30 degrees between the four radial regions.
Code: Select all
def radial_buttons(overlap, key1, key2, key3, key4):
global x
global y
global theta
global mb_active
offset = 90
min_swipe = 10
x += mouse.deltaX
y += mouse.deltaY
if (x*x + y*y) > min_swipe*min_swipe:
mb_active = True
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
keyboard.setKey(key1, mb_active and (theta < 45 + ol or theta > 315 - ol))
keyboard.setKey(key2, mb_active and (theta > 45 - ol and theta < 135 + ol))
keyboard.setKey(key3, mb_active and (theta > 135 - ol and theta < 225 + ol))
keyboard.setKey(key4, mb_active and (theta > 225 - ol and theta < 315 + ol))
diagnostics.watch(x)
diagnostics.watch(y)
diagnostics.watch(theta)
diagnostics.watch(mb_active)
if starting:
x = 0
y = 0
theta = -1
mb_active = False
mwu_flag = False
mwd_flag = False
s = True
if s:
if mouse.wheelUp:
if mwu_flag:
mwu_flag = False
else:
mwu_flag = True
if mouse.wheelDown:
if mwd_flag:
mwd_flag = False
else:
mwd_flag = True
if mouse.leftButton:
radial_buttons(-15, Key.A, Key.B, Key.C, Key.D)
elif mouse.rightButton:
radial_buttons(-15, Key.E, Key.F, Key.G, Key.H)
elif mwu_flag:
radial_buttons(-15, Key.I, Key.J, Key.K, Key.L)
elif mwd_flag:
radial_buttons(-15, Key.M, Key.N, Key.O, Key.P)
else:
x = 0
y = 0
if mb_active:
mb_active = False
radial_buttons(-15, Key.A, Key.B, Key.C, Key.D)
radial_buttons(-15, Key.E, Key.F, Key.G, Key.H)
radial_buttons(-15, Key.I, Key.J, Key.K, Key.L)
radial_buttons(-15, Key.M, Key.N, Key.O, Key.P)
Re: Mouse To Keyboard Script
Posted: Thu Dec 08, 2016 5:07 am
by Dylate
This script works so much better. Only few things I cannot understand:
1. I can't move semi-direction, & can't fix this. Like NE, NW direction not work.
2. With left-right button triggers are being pressed for long , but I want only beying pressed once. Create own function but how trigger setPressed like in your function don't understand...
Re: Mouse To Keyboard Script
Posted: Thu Dec 08, 2016 6:09 am
by zelmon64
I'm glad you like it
.
Dylate wrote:
1. I can't move semi-direction, & can't fix this. Like NE, NW direction not work.
2. With left-right button triggers are being pressed for long , but I want only beying pressed once. Create own function but how trigger setPressed like in your function don't understand...
1. If you want NW to press both N and W change the overlap value to a positive number (currently all set to
-15: radial_buttons(
-15, Key.A, Key.B, Key.C, Key.D)). For example, if you want a
30 degree overlap put
15: radial_buttons(
15, Key.A, Key.B, Key.C, Key.D) (adds 15 degrees to
both sides). I initially miss understood and thought you wanted it to press neither which is why I put -15.
2. Is it that you want the key to be pressed
a) when the physical button is release or
b) when the mouse is first dragged into the corresponding region? I thought you wanted it held for WASD movement but I can see how it would be awkward for other things. Tell me which situation you would like (or one I haven't thought of) and I will add it.
Re: Mouse To Keyboard Script
Posted: Thu Dec 08, 2016 6:57 am
by Dylate
I don't have keyboard, typing from virtual. And I want play some action game, but buttons from mouse not enough. You script right now work for LBM&RBM like i want it, but it holds buttons. I want when LBM&RBM trigger setPressed event not hold for next iteration.
I usually use programm called StrokesPlus for gestures. This programm let me create button for gestures when I hold RBM. But with this programm FreePie works not correct, starts spamming trigger button for StrokesPlus and I need close Strokes. And I want 4 buttons from Strokes add to this scripts. When I hold RBM and move cursor programm send button only once. When I add simple mose.deltaX(Y) < (>) 0 keyPressed(key) script not work properly, often sends 2-3 buttons with semi-direction.
One more question, I assign buttons when holds RBM and press LBM sends keyPressed, Can I assign button when I hold LBM and pressed RBM sends other then previus one? If yes how did this. I try with flags but nothing work for me...
UPDATE: I'm doing something wrong or FreePie conflicting with some programm, script works like a charm. Now starting testing with some my childhood game.
Re: Mouse To Keyboard Script
Posted: Thu Dec 08, 2016 7:48 am
by zelmon64
Dylate wrote:UPDATE: I'm doing something wrong or FreePie conflicting with some programm, script works like a charm. Now starting testing with some my childhood game.
I'm glad it works now. I added the new functions anyway so that it should be more versatile:
radial_buttons_initial will press a key when the mouse moves into that region and
radial_buttons_release will press a key when the mouse button is released (it requires extra flags).
I'll have a think about the multi-button gestures.
Code: Select all
def radial_buttons_hold(overlap, key1, key2, key3, key4):
global x
global y
global theta
global mb_active
offset = 90
min_swipe = 10
x += mouse.deltaX
y += mouse.deltaY
if (x*x + y*y) > min_swipe*min_swipe:
mb_active = True
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
keyboard.setKey(key1, mb_active and (theta < 45 + ol or theta > 315 - ol))
keyboard.setKey(key2, mb_active and (theta > 45 - ol and theta < 135 + ol))
keyboard.setKey(key3, mb_active and (theta > 135 - ol and theta < 225 + ol))
keyboard.setKey(key4, mb_active and (theta > 225 - ol and theta < 315 + ol))
def radial_buttons_initial(overlap, key1, key2, key3, key4):
global x
global y
global theta
global mb_active
offset = 90
min_swipe = 10
x += mouse.deltaX
y += mouse.deltaY
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
if (x*x + y*y) > min_swipe*min_swipe and not mb_active:
mb_active = True
#keyboard.setPressed(key1, mb_active and (theta < 45 + ol or theta > 315 - ol))
#keyboard.setPressed(key2, mb_active and (theta > 45 - ol and theta < 135 + ol))
#keyboard.setPressed(key3, mb_active and (theta > 135 - ol and theta < 225 + ol))
#keyboard.setPressed(key4, mb_active and (theta > 225 - ol and theta < 315 + ol))
if theta < 45 + ol or theta > 315 - ol:
keyboard.setPressed(key1)
if theta > 45 - ol and theta < 135 + ol:
keyboard.setPressed(key2)
if theta > 135 - ol and theta < 225 + ol:
keyboard.setPressed(key3)
if theta > 225 - ol and theta < 315 + ol:
keyboard.setPressed(key4)
def radial_buttons_release(overlap, key1, key2, key3, key4, mb_release):
global x
global y
global theta
global mb_active
offset = 90
min_swipe = 10
x += mouse.deltaX
y += mouse.deltaY
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
if (x*x + y*y) > min_swipe*min_swipe and not mb_active:
mb_active = True
elif mb_active:
if mb_release:
if theta < 45 + ol or theta > 315 - ol:
keyboard.setPressed(key1)
if theta > 45 - ol and theta < 135 + ol:
keyboard.setPressed(key2)
if theta > 135 - ol and theta < 225 + ol:
keyboard.setPressed(key3)
if theta > 225 - ol and theta < 315 + ol:
keyboard.setPressed(key4)
return False
else:
return True
else:
return False
def release_buttons(key1, key2, key3, key4):
keyboard.setKeyUp(key1)
keyboard.setKeyUp(key2)
keyboard.setKeyUp(key3)
keyboard.setKeyUp(key4)
if starting:
x = 0
y = 0
theta = -1
mb_active = False
mwu_flag = False
mwd_flag = False
s = True
lmb_release_flag = False
mwd_release_flag = False
if s:
diagnostics.watch(x)
diagnostics.watch(y)
diagnostics.watch(theta)
diagnostics.watch(mb_active)
diagnostics.watch(mwu_flag)
diagnostics.watch(mwd_flag)
diagnostics.watch(lmb_release_flag)
diagnostics.watch(mwd_release_flag)
if mouse.wheelUp:
if mwu_flag:
mwu_flag = False
else:
mwu_flag = True
if mouse.wheelDown:
if mwd_flag:
mwd_flag = False
else:
mwd_flag = True
if mouse.leftButton:
#radial_buttons_initial(15, Key.A, Key.B, Key.C, Key.D)
lmb_release_flag = radial_buttons_release(15, Key.A, Key.B, Key.C, Key.D, False)
elif mouse.rightButton:
radial_buttons_initial(15, Key.E, Key.F, Key.G, Key.H)
elif mwu_flag:
radial_buttons_hold(-15, Key.I, Key.J, Key.K, Key.L)
elif mwd_flag:
#radial_buttons_hold(-15, Key.M, Key.N, Key.O, Key.P)
mwd_release_flag = radial_buttons_release(15, Key.M, Key.N, Key.O, Key.P, False)
else:
if mb_active:
# activate keys on button release
radial_buttons_release(15, Key.A, Key.B, Key.C, Key.D, lmb_release_flag)
lmb_release_flag = False
radial_buttons_release(15, Key.M, Key.N, Key.O, Key.P, mwd_release_flag)
mwd_release_flag = False
# ensure all keys are released
release_buttons(Key.A, Key.B, Key.C, Key.D)
release_buttons(Key.E, Key.F, Key.G, Key.H)
release_buttons(Key.I, Key.J, Key.K, Key.L)
release_buttons(Key.M, Key.N, Key.O, Key.P)
mb_active = False
x = 0
y = 0
Re: Mouse To Keyboard Script
Posted: Thu Dec 08, 2016 9:35 am
by Dylate
Work great!!! Only add this:
if math.fabs(x) > 100 or math.fabs(x) > 100:
x = 0
y = 0
'Cause in game x & y can be too high for quick reduce. And change min_swipe for better response. Right now only step before for perfect script. How combine radial_buttons_hold with buttons triggers? Right now seems work separetely.
Re: Mouse To Keyboard Script
Posted: Thu Dec 08, 2016 12:36 pm
by zelmon64
Dylate wrote:Work great!!! Only add this:
if math.fabs(x) > 100 or math.fabs(x) > 100:
x = 0
y = 0
'Cause in game x & y can be too high for quick reduce. And change min_swipe for better response. Right now only step before for perfect script. How combine radial_buttons_hold with buttons triggers? Right now seems work separetely.
I added your limit on x and y and made min_swipe a global variable so that it only has to be changed in one place.
I've had a go at combining buttons and in the script below the LBM can either be held as normal or pressed when the wheeldown is active (which also deactivates the wheeldown).
Code: Select all
def radial_buttons_hold(overlap, key1, key2, key3, key4):
global x
global y
global theta
global mb_active
global min_swipe
offset = 90
x += mouse.deltaX
y += mouse.deltaY
if (x*x + y*y) > min_swipe*min_swipe:
mb_active = True
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
keyboard.setKey(key1, mb_active and (theta < 45 + ol or theta > 315 - ol))
keyboard.setKey(key2, mb_active and (theta > 45 - ol and theta < 135 + ol))
keyboard.setKey(key3, mb_active and (theta > 135 - ol and theta < 225 + ol))
keyboard.setKey(key4, mb_active and (theta > 225 - ol and theta < 315 + ol))
def radial_buttons_initial(overlap, key1, key2, key3, key4):
global x
global y
global theta
global mb_active
global min_swipe
offset = 90
x += mouse.deltaX
y += mouse.deltaY
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
if (x*x + y*y) > min_swipe*min_swipe and not mb_active:
mb_active = True
if theta < 45 + ol or theta > 315 - ol:
keyboard.setPressed(key1)
if theta > 45 - ol and theta < 135 + ol:
keyboard.setPressed(key2)
if theta > 135 - ol and theta < 225 + ol:
keyboard.setPressed(key3)
if theta > 225 - ol and theta < 315 + ol:
keyboard.setPressed(key4)
def radial_buttons_holdnpress(overlap, key1, key2, key3, key4):
global mb_active
ol = overlap
if mb_active:
if theta < 45 + ol or theta > 315 - ol:
keyboard.setPressed(key1)
if theta > 45 - ol and theta < 135 + ol:
keyboard.setPressed(key2)
if theta > 135 - ol and theta < 225 + ol:
keyboard.setPressed(key3)
if theta > 225 - ol and theta < 315 + ol:
keyboard.setPressed(key4)
def radial_buttons_release(overlap, key1, key2, key3, key4, mb_release):
global x
global y
global theta
global mb_active
global min_swipe
offset = 90
x += mouse.deltaX
y += mouse.deltaY
if x > 0:
theta = math.atan(y/x) * 180 / math.pi + offset
elif x < 0:
theta = ( math.atan(y/x) + math.pi ) * 180 / math.pi + offset
else:
if y > 0:
theta = 90 + offset
elif y < 0:
theta = -90 + offset
else:
theta = -91 + offset
ol = overlap
if (x*x + y*y) > min_swipe*min_swipe and not mb_active:
mb_active = True
elif mb_active:
if mb_release:
if theta < 45 + ol or theta > 315 - ol:
keyboard.setPressed(key1)
if theta > 45 - ol and theta < 135 + ol:
keyboard.setPressed(key2)
if theta > 135 - ol and theta < 225 + ol:
keyboard.setPressed(key3)
if theta > 225 - ol and theta < 315 + ol:
keyboard.setPressed(key4)
return False
else:
return True
else:
return False
def release_buttons(key1, key2, key3, key4):
keyboard.setKeyUp(key1)
keyboard.setKeyUp(key2)
keyboard.setKeyUp(key3)
keyboard.setKeyUp(key4)
if starting:
min_swipe = 5
x = 0
y = 0
theta = -1
mb_active = False
mwu_flag = False
mwd_flag = False
s = True
lmb_release_flag = False
mwd_release_flag = False
if s:
diagnostics.watch(x)
diagnostics.watch(y)
diagnostics.watch(theta)
diagnostics.watch(mb_active)
diagnostics.watch(mwu_flag)
diagnostics.watch(mwd_flag)
diagnostics.watch(lmb_release_flag)
diagnostics.watch(mwd_release_flag)
if mouse.wheelUp:
if mwu_flag:
mwu_flag = False
else:
mwu_flag = True
if mouse.wheelDown:
if mwd_flag:
mwd_flag = False
else:
mwd_flag = True
if mouse.rightButton:
radial_buttons_initial(15, Key.E, Key.F, Key.G, Key.H)
elif mwu_flag:
radial_buttons_hold(-15, Key.I, Key.J, Key.K, Key.L)
elif mwd_flag:
#radial_buttons_hold(-15, Key.M, Key.N, Key.O, Key.P)
mwd_release_flag = radial_buttons_release(15, Key.M, Key.N, Key.O, Key.P, False)
if mouse.leftButton:
lmb_release_flag = True
elif lmb_release_flag:
radial_buttons_holdnpress(15, Key.Q, Key.R, Key.S, Key.T)
lmb_release_flag = False
mwd_release_flag = False
mwd_flag = False
elif mouse.leftButton:
radial_buttons_hold(15, Key.A, Key.B, Key.C, Key.D)
else:
if mb_active:
# activate keys on button release
#radial_buttons_release(15, Key.A, Key.B, Key.C, Key.D, lmb_release_flag)
#lmb_release_flag = False
radial_buttons_release(15, Key.M, Key.N, Key.O, Key.P, mwd_release_flag)
mwd_release_flag = False
# ensure all keys are released
release_buttons(Key.A, Key.B, Key.C, Key.D)
release_buttons(Key.E, Key.F, Key.G, Key.H)
release_buttons(Key.I, Key.J, Key.K, Key.L)
release_buttons(Key.M, Key.N, Key.O, Key.P)
mb_active = False
x = 0
y = 0
if math.fabs(x) > 100 or math.fabs(y) > 100:
x = 0
y = 0