# How to introduce in a script the action of a key?

In this question, I prefer to program the action of a key by the python because even the use of the sensor keybaord in the BGE does not allow me to realize this

So I write this little python script that has errors:

import bpy
import bge

#Place the reference of the selected 3D object in a variable
my_object = bpy.context.object

tre = bge.logic.getCurrentController ()

my_object = tre.owner

keyboard = bge.logic.keyboard

wKey = bge.logic.KX_INPUT_ACTIVE == keyboard.events [bge.events.WKEY]

#We fix the object to the desired position
my_object.location [0] = 7

my_object.location [1] = 0

frame_num = 0

between_each_keyframe = 1

bpy.context.scene.frame_start = 0

bpy.context.scene.frame_end = 135000

k = 0

if wKey:

k+ = 1

for n in range (0,6300):

x = -n

y = k * n * 0.04

# We place ourselves on the right frame, frame_num worth 0/50 at the beginning
bpy.context.scene.frame_set (frame_num)

mon_objet.location = (x, y, 0)

# We increase the value of the next frame to fix
frame_num = frame_num + entre_chaque_keyframe


The simple goal of this script is to generate on an object a parametric equation trajectory:

x = t
y = 0.04 * t * k


(E) with the addition of pressing a key on the keyboard (Y for example) increments the moment of the animation k by 1: so at each press, the trajectory should be deflected rectilinearly.

Could we please correct this script in order to realize (E)?

Thank you for your help

• Suggest not mixing bpy and bge What is your expected outcome of running bpy.context.scene.frame_set (frame_num) in the game engine for example? If run in blender (not in game engine) then import bge will be an issue. – batFINGER Sep 17 at 14:24