3
$\begingroup$

I want to launch vlc.exe when I hit spacebar in the BGE.

I have this script and it does the job:

import subprocess subprocess.call(['VLC\VLC.exe']) 

The problem now, is that the script is invoked twice. It launches vlc and when I close the vlc instance, it launches it one more time. (I tried also with Tap option in the sensor)

$\endgroup$

3 Answers 3

4
$\begingroup$

This happens because there are two events for key down and key up. To choose one of them replace the print statement with your code to invoke and delete the other unneeded lines.

Script to test the behavior:

import bge cont = bge.logic.getCurrentController() sensor = cont.sensors['Space'] # Assumes that keyboard sensor is named as 'Space' if sensor.getKeyStatus(bge.events.SPACEKEY) == bge.logic.KX_INPUT_JUST_ACTIVATED: print("sensor down") if bge.logic.keyboard.events[bge.events.SPACEKEY] ==bge.logic.KX_INPUT_JUST_ACTIVATED: print("Down") if sensor.getKeyStatus(bge.events.SPACEKEY) == bge.logic.KX_INPUT_JUST_RELEASED: print("Up") 

One event picked

import bge import subprocess if bge.logic.keyboard.events[bge.events.SPACEKEY] ==bge.logic.KX_INPUT_JUST_ACTIVATED: subprocess.call(['C:\\path\\app.exe']) 
$\endgroup$
2
$\begingroup$

This occurs not just with key presses but all Blender sensors. All sensors send at least 2 pulses, a positive and then a negative.

sensor = bge.logic.getCurrentController.owner["Sensor"] if sensor.positive: Do something 
$\endgroup$
0
$\begingroup$

just check for sensor.positive

so your code should be:

import subprocess, bge if bge.logic.getCurrentController().sensors["sensor"].positive: subprocess.call(['VLC\VLC.exe']) 
$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.