Script to press key each 60 seconds

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

Script to press key each 60 seconds

Post by firewater »

First of all I'm confused about this FreePIE software. I'm currently using "GlovePIE", or so says the name of the folder, but the name of the actual .exe is PIEFree.exe so I'm not sure what I'm using?

Image

I downloaded the .msi file on the official site and it looks nothing like whatever I'm using. Also I couldn't load the script pictured above on FreePIE because it doesn't recognize .PIE files or so it seems ???

Anyway, beside that confusion and assuming this is a similar software, I need help making a small script. What I need to do is that when I press certain key combination (Alt + P) a key (P) is pressed each 60 seconds until I press Alt + P again. I know this may not be what FreePIE focuses on, but I tried doing this with AHK and it didn't work because I'm trying to run this on a game emulator and since it uses direct input there was no way it recognized the keys being sent (or that's what I gathered).

I beg you to help me because the GlovePIE forums are dead and this seems to be an in development continuation of that project.
TiagoTiago
One Eyed Hopeful
Posts: 33
Joined: Thu Mar 13, 2014 1:49 am

Re: Script to press key each 60 seconds

Post by TiagoTiago »

firewater wrote:First of all I'm confused about this FreePIE software. I'm currently using "GlovePIE", or so says the name of the folder, but the name of the actual .exe is PIEFree.exe so I'm not sure what I'm using?

Image

I downloaded the .msi file on the official site and it looks nothing like whatever I'm using. Also I couldn't load the script pictured above on FreePIE because it doesn't recognize .PIE files or so it seems ???

Anyway, beside that confusion and assuming this is a similar software, I need help making a small script. What I need to do is that when I press certain key combination (Alt + P) a key (P) is pressed each 60 seconds until I press Alt + P again. I know this may not be what FreePIE focuses on, but I tried doing this with AHK and it didn't work because I'm trying to run this on a game emulator and since it uses direct input there was no way it recognized the keys being sent (or that's what I gathered).

I beg you to help me because the GlovePIE forums are dead and this seems to be an in development continuation of that project.

I don't got time to figure out a script right now. But I can tell you what you got on that screenshot is GlovePIE, and not FreePIE.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Script to press key each 60 seconds

Post by CyberVillain »

Code: Select all

from threading import Timer

if starting: 
	t = None
	interval = 5
	key = Key.P
	enable = False	
	
if stopping:
	cancel()	

def update():
	if enable:
		diagnostics.debug("Pressing key")
		keyboard.setPressed(key)	
		t = Timer(interval, update)
		t.start()

def cancel():
	diagnostics.debug("canceling")
	if t:
		t.cancel() #Does nothing?
	
if keyboard.getKeyDown(Key.LeftAlt) and keyboard.getPressed(Key.P):	
	diagnostics.debug("toggling enable")		
	enable = not enable
	if not enable:
		diagnostics.debug("disabling")
		cancel()
		
	if enable:
		diagnostics.debug("enabling")
		t = Timer(interval, update)
		t.start()
Timers in python seems a bit messy but here goes. I should maybe create more helper methods for this
konstantin_lozev
Cross Eyed!
Posts: 192
Joined: Fri Jul 04, 2014 1:43 am

Re: Script to press key each 60 seconds

Post by konstantin_lozev »

Great example, maybe can be added in the GitHub examples.
I must admit I would have never looked into 16.2.1. Thread Objects for it in the reference, but my Python is level 0.01 :)
It was also weird to see update called from within itself (sorry if I misunderstood...)
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Script to press key each 60 seconds

Post by CyberVillain »

Since its spawning threads and FreePIE is not marshalling these (yet) i might dont want to put it there just yet (It has to be regarded as do it on your own risk until FreePIE can detect and kill threads created by scripts when you stop runnning).

It's actually not called from itself its just referenced, its called from the Timer thread by the Timer class

calling itself is otherwise a pretty common case, its called recursion and is used alot in pograming like

Code: Select all

def getBaseClass(class) 
   if class.base:
      return getBaseClass(class.base)

   return class
This code will recusrivly look until it finds the base class lowest down in the chain, its just pseduo code, I do not know if pythin has a class.base property. But I think you get the idea
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Script to press key each 60 seconds

Post by CyberVillain »

Here is a version that uses the .Net timer instead, much nicer synstax

Code: Select all

from System.Timers import Timer

def update(s, e):
	diagnostics.debug("Pressing key")
	keyboard.setPressed(key)

if starting:    
   interval = 5000
   t = Timer(interval)
   t.Elapsed += update
   key = Key.P
   enable = False  
  
if stopping:
   cancel()

def cancel():
   diagnostics.debug("canceling")
   t.Stop()

if keyboard.getKeyDown(Key.LeftAlt) and keyboard.getPressed(Key.P):   
	diagnostics.debug("toggling enable")      
	enable = not enable
	if not enable:
		diagnostics.debug("disabling")
		cancel()
      
	if enable:
		diagnostics.debug("enabling")
		t.Start()
konstantin_lozev
Cross Eyed!
Posts: 192
Joined: Fri Jul 04, 2014 1:43 am

Re: Script to press key each 60 seconds

Post by konstantin_lozev »

CyberVillain wrote:Since its spawning threads and FreePIE is not marshalling these (yet) i might dont want to put it there just yet (It has to be regarded as do it on your own risk until FreePIE can detect and kill threads created by scripts when you stop runnning).

It's actually not called from itself its just referenced, its called from the Timer thread by the Timer class

calling itself is otherwise a pretty common case, its called recursion and is used alot in pograming like

Code: Select all

def getBaseClass(class) 
   if class.base:
      return getBaseClass(class.base)

   return class
This code will recusrivly look until it finds the base class lowest down in the chain, its just pseduo code, I do not know if pythin has a class.base property. But I think you get the idea
For the thread handling problem, would it not be safer then to add up the equivalent of delta(timestamp) every frame and check whether this exceeded a preset time? In theory, you don't even need to add it every frame.
And for calling the function from within itself, that must be my scripter limited programming understanding, I think of functions the way that you first define them and you call them separately, that the interpreter does not execute anything within a function untill it's called from outside. Will look into your example.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Script to press key each 60 seconds

Post by CyberVillain »

Yes, but I cant guarantee that the users do that, so FreePIE must be able to marshal threads created by the scripts. But the only bullet proof way of doing that is to run the script engine in its own process and kill it when you stop the script, this means that i cant inmemory communicate between the GUI and the script engine etc, so I need to use memory pipes. Its not on my priority list :D
konstantin_lozev
Cross Eyed!
Posts: 192
Joined: Fri Jul 04, 2014 1:43 am

Re: Script to press key each 60 seconds

Post by konstantin_lozev »

I am not sure if I explained well. I thought this could be made alternatively in 2 other ways:
- adding up a timestamp delta [sorry for spamming GlovePIE code here, I am still struggling with FreePIE API and Python]
----------------------------------------------
var.timepassed += delta(timestamp)

if (var.timepassed > 5s) {
key.P = true
var.timepassed = 0
} else {
Key.P = false
}
----------------------------------------------

- simply using the GlovePIE Framerate for counting the time passed
----------------------------------------------
var.maxtime = 5
var.maxtimeframes = PIE.framerate*var.maxtime
if (var.counter > var.maxtimeframes) {
key.P = true
var.counter = 0
} else {
Key.P = false
var.counter +=1
}
----------------------------------------------

Sorry if there are any mistakes, I wrote this on the fly.
Last edited by konstantin_lozev on Fri Jan 16, 2015 9:22 am, edited 1 time in total.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Script to press key each 60 seconds

Post by CyberVillain »

It wont be very accurate, anyway, FreePIE still needs to handle if user creates threads thats all im saying and why I might should posting it on the example page just yet since its "Run it but it might break FreePIE".
konstantin_lozev
Cross Eyed!
Posts: 192
Joined: Fri Jul 04, 2014 1:43 am

Re: Script to press key each 60 seconds

Post by konstantin_lozev »

OK
trabitboy
One Eyed Hopeful
Posts: 4
Joined: Thu Jan 22, 2015 4:37 am

Re: Script to press key each 60 seconds

Post by trabitboy »

CyberVillain wrote:Here is a version that uses the .Net timer instead, much nicer synstax

Code: Select all

from System.Timers import Timer

def update(s, e):
	diagnostics.debug("Pressing key")
	keyboard.setPressed(key)

if starting:    
   interval = 5000
   t = Timer(interval)
   t.Elapsed += update
   key = Key.P
   enable = False  
  
if stopping:
   cancel()

def cancel():
   diagnostics.debug("canceling")
   t.Stop()

if keyboard.getKeyDown(Key.LeftAlt) and keyboard.getPressed(Key.P):   
	diagnostics.debug("toggling enable")      
	enable = not enable
	if not enable:
		diagnostics.debug("disabling")
		cancel()
      
	if enable:
		diagnostics.debug("enabling")
		t.Start()
works very well for me , thanks for that :)
a note to python beginners like me :
copy pasting replaces tabs with spaces, restore tabs
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Script to press key each 60 seconds

Post by CyberVillain »

trabitboy wrote:
CyberVillain wrote:Here is a version that uses the .Net timer instead, much nicer synstax
works very well for me , thanks for that :)
a note to python beginners like me :
copy pasting replaces tabs with spaces, restore tabs
Here is a version with hot key predicate parameterized

Code: Select all

from System.Timers import Timer

def update(s, e):
	diagnostics.debug("Pressing key")
	keyboard.setPressed(key)

if starting:    
   interval = 5000
   t = Timer(interval)
   t.Elapsed += update
   key = Key.P
   hotkey = lambda: keyboard.getKeyDown(Key.LeftAlt) and keyboard.getPressed(Key.P)
   enable = False  
  
if stopping:
   cancel()

def cancel():
   diagnostics.debug("canceling")
   t.Stop()

if hotkey():   
	diagnostics.debug("toggling enable")      
	enable = not enable
	if not enable:
		diagnostics.debug("disabling")
		cancel()
      
	if enable:
		diagnostics.debug("enabling")
		t.Start()
Post Reply

Return to “FreePIE”