Help with simple script to input keys

Official forum for open source FreePIE discussion and development.
Post Reply
firewater
One Eyed Hopeful
Posts: 6
Joined: Tue Sep 30, 2014 3:31 pm

Help with simple script to input keys

Post by firewater »

I've been using GlovePIE for maybe a year now but it just isn't working anymore. It takes about 20 minutes of clicking Run/Stop for it to connect almost every single time I try and that's at least once a day so I've had enough. I thought it was the Blueetooth adapter or the Wiimote itself, so I was really bummed, but out of any options and with nothing to lose I downloaded FreePIE and it connects instantly!!! The problem though is that I can't do anything with it because I don't know how to script and all the scripts I find are either super fancy and don't do what I want or seem tor require motion plus, which I don't have. All I need is to use my Wiimote as a media controller, so I just want a very simple script that shows how to set up each key to a button, like Key.Space be the 1 button and a Key.Alt and Key.+ be the up arrow...

I imagine it's something like this:
def wiimoteButtonUpdates:
# A, B buttons
mouse.leftButton = wiimote[0].buttons.button_down(WiimoteButtons.B)
keyboard.setKey( Key.R, wiimote[0].buttons.button_down(WiimoteButtons.A) )

# Dpad controls
keyboard.setKey( Key.D1, wiimote[0].buttons.button_down(WiimoteButtons.DPadUp) )
keyboard.setKey( Key.D2, wiimote[0].buttons.button_down(WiimoteButtons.DPadDown) )
keyboard.setKey( Key.D3, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )
keyboard.setKey( Key.D4, wiimote[0].buttons.button_down(WiimoteButtons.DPadRight) )

# 1,2 buttons
keyboard.setKey( Key.LeftShift, wiimote[0].buttons.button_down(WiimoteButtons.One) )
keyboard.setKey( Key.Q, wiimote[0].buttons.button_down(WiimoteButtons.Two) )

def nunchuckUpdate():
# Nunchuck stick update
y = wiimote[0].nunchuck.stick.y
x = wiimote[0].nunchuck.stick.x

keyboard.setKey(Key.W, (y >= stickDeadZone))
keyboard.setKey(Key.S, (y <= -stickDeaZone))
keyboard.setKey(Key.D, (x >= stickDeadZone))
keyboard.setKey(Key.A, (x <= -stickDeadZone))

# Nuncuck C Z buttons
keyboard.setKey( Key.Space, wiimote[0].nunchuck.buttons.button_down(NunchuckButtons.C) )
keyboard.setKey( Key.E, wiimote[0].nunchuck.buttons.button_down(NunchuckButtons.Z) )


if starting:
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 2

stickDeadZone = 0.20

wiimote[0].buttons.update += wiimoteButtonUpdates
wiimote[0].nunchuck.update += nunchuckUpdate
I got that from another thread, but first, I don't even need to use the Nunchuk and second, it doesn't work because I guess the message Unexpected Token ":"...
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Help with simple script to input keys

Post by CyberVillain »

Welcome firewater, our bluetooth stack uses Dolphimote code and yes, it works pretty well :D

Python does not play nice with new beginners I'm afraid, its because it uses whitespaces / tabs as indentation for functions and other scopes

So you need to fix that like

Code: Select all

def wiimoteButtonUpdates:
   # A, B buttons
   mouse.leftButton = wiimote[0].buttons.button_down(WiimoteButtons.B)
   keyboard.setKey( Key.R, wiimote[0].buttons.button_down(WiimoteButtons.A) )

   # Dpad controls
   keyboard.setKey( Key.D1, wiimote[0].buttons.button_down(WiimoteButtons.DPadUp) )
   keyboard.setKey( Key.D2, wiimote[0].buttons.button_down(WiimoteButtons.DPadDown) )
   keyboard.setKey( Key.D3, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )
   keyboard.setKey( Key.D4, wiimote[0].buttons.button_down(WiimoteButtons.DPadRight) )

   # 1,2 buttons
   keyboard.setKey( Key.LeftShift, wiimote[0].buttons.button_down(WiimoteButtons.One) )
   keyboard.setKey( Key.Q, wiimote[0].buttons.button_down(WiimoteButtons.Two) )
All lines of code above will now belong to the wiimoteButtonUpdates function

In the same manner your if starting: code is wrong too

Code: Select all

if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 2

   stickDeadZone = 0.20

   wiimote[0].buttons.update += wiimoteButtonUpdates
   wiimote[0].nunchuck.update += nunchuckUpdate
firewater
One Eyed Hopeful
Posts: 6
Joined: Tue Sep 30, 2014 3:31 pm

Re: Help with simple script to input keys

Post by firewater »

Code: Select all

def wiimoteButtonUpdates:
   #Volume
   keyboard.setKey( Key.Alt, and Key.Equals, wiimote[0].buttons.button_down(WiimoteButtons.DPadRigh) )
   keyboard.setKey( Key.Alt, and Key.Minus, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )
   
   #Timeline
   keyboard.setKey( Key.LeftArrow, wiimote[0].buttons.button_down(WiimoteButtons.DPadUp) )
   keyboard.setKey( Key.RightArrow, wiimote[0].buttons.button_down(WiimoteButtons.DPadDown) )
   keyboard.setKey( Key.Space, wiimote[0].buttons.button_down(WiimoteButtons.One) )
   
   #Time
   keyboard.setKey( Key.Enter, wiimote[0].buttons.button_down(WiimoteButtons.B) )
   keyboard.setKey( Key.Alt, and Key.Ctrl, wiimote[0].buttons.button_down(WiimoteButtons.Home) )

   #Frames
   keyboard.setKey( Key.Ctrl, and Key.E wiimote[0].buttons.button_down(WiimoteButtons.Two) )
   keyboard.setKey( Key.Ctrl, and Key.A wiimote[0].buttons.button_down(WiimoteButtons.A) )
   
   #Subtitles
   keyboard.setKey( Key.Alt, and Key.H wiimote[0].buttons.button_down(WiimoteButtons.Minus) )
   
if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 2
   wiimote[0].buttons.update += wiimoteButtonUpdates
Thanks, this is what I've got now, but I'm still getting the Unexpected Token ":" and the script won't run (though the Wiimote connects anyway).
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Help with simple script to input keys

Post by CyberVillain »

The correct function declaration looks like this

Code: Select all

def wiimoteButtonUpdates():
Sorry, missed that the first time
firewater
One Eyed Hopeful
Posts: 6
Joined: Tue Sep 30, 2014 3:31 pm

Re: Help with simple script to input keys

Post by firewater »

Alright, thanks. Well I'm still getting it to function. This is what I have now:

Code: Select all

def wiimoteButtonUpdates():

   #Volume
   keyboard.setKey( Key.Alt, wiimote[0].buttons.button_down(WiimoteButtons.DPadRight) )
   keyboard.setKey( Key.Equals, wiimote[0].buttons.button_down(WiimoteButtons.DPadRight) )
   keyboard.setKey( Key.Alt, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )
   keyboard.setKey( Key.Minus, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )

   #Timeline
   keyboard.setKey( Key.LeftArrow, wiimote[0].buttons.button_down(WiimoteButtons.DPadUp) )
   keyboard.setKey( Key.RightArrow, wiimote[0].buttons.button_down(WiimoteButtons.DPadDown) )
   keyboard.setKey( Key.Space, wiimote[0].buttons.button_down(WiimoteButtons.One) )
   
   #Time
   keyboard.setKey( Key.Enter, wiimote[0].buttons.button_down(WiimoteButtons.B) )
   keyboard.setKey( Key.Ctrl, wiimote[0].buttons.button_down(WiimoteButtons.Home) )
   keyboard.setKey( Key.Alt, wiimote[0].buttons.button_down(WiimoteButtons.Home) )
   
   #Frames
   keyboard.setKey( Key.Ctrl, wiimote[0].buttons.button_down(WiimoteButtons.Two) )
   keyboard.setKey( Key.E wiimote[0].buttons.button_down(WiimoteButtons.Two) )
   keyboard.setKey( Key.A wiimote[0].buttons.button_down(WiimoteButtons.A) )
   keyboard.setKey( Key.Ctrl, wiimote[0].buttons.button_down(WiimoteButtons.A) )
   
   #Subtitles
   keyboard.setKey( Key.H, wiimote[0].buttons.button_down(WiimoteButtons.Minus) )
   keyboard.setKey( Key.Alt, wiimote[0].buttons.button_down(WiimoteButtons.Minus) )
   
if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 2
   wiimote[0].buttons.update += wiimoteButtonUpdates
I'm getting unexpected token 'wiimote' as soon as the Wiimote connects and then the script stops running.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Help with simple script to input keys

Post by CyberVillain »

See below
Last edited by CyberVillain on Mon Mar 16, 2015 7:18 am, edited 1 time in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Help with simple script to input keys

Post by CyberVillain »

You are missing commas on these two lines

Code: Select all

keyboard.setKey( Key.E wiimote[0].buttons.button_down(WiimoteButtons.Two) )
keyboard.setKey( Key.A wiimote[0].buttons.button_down(WiimoteButtons.A) )
firewater
One Eyed Hopeful
Posts: 6
Joined: Tue Sep 30, 2014 3:31 pm

Re: Help with simple script to input keys

Post by firewater »

Damn, those always pass through! I fixed them and there's no more error in the console, but it's still not working :( It connects and the script runs, but none of the buttons do anything...
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Help with simple script to input keys

Post by CyberVillain »

IF you put diagnostics.debug("Foo") in the wiimoteButtonUpdates function does it trigger?
firewater
One Eyed Hopeful
Posts: 6
Joined: Tue Sep 30, 2014 3:31 pm

Re: Help with simple script to input keys

Post by firewater »

I'm not sure if I know how to do that. I don't know how to call the Foo thing.

Code: Select all

def wiimoteButtonUpdates(): 

   diagnostics.debug("Foo")
   
   #Volume
   keyboard.setKey( Key.Alt, wiimote[0].buttons.button_down(WiimoteButtons.DPadRight) )
   keyboard.setKey( Key.Equals, wiimote[0].buttons.button_down(WiimoteButtons.DPadRight) )
   keyboard.setKey( Key.Alt, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )
   keyboard.setKey( Key.Minus, wiimote[0].buttons.button_down(WiimoteButtons.DPadLeft) )
I feel like the key to making this work lies somewhere around

Code: Select all

if starting:
   system.setThreadTiming(TimingTypes.HighresSystemTimer)
   system.threadExecutionInterval = 2
   wiimote[0].buttons.update += wiimoteButtonUpdates
As I have no clue to what any of those do. As I said, I copied the code from another post (it was the closest thing I could find to what I want to do) and modified it a bit.
Post Reply

Return to “FreePIE”