reading input from the keyboard using python and no logic blocks

Hi,

I am trying to read from the keyboard without using logic blocks (see my other question for why…). When i use “x = raw_input()” activated by an “always” sensor the game freezes. how can i make it wait quietly in the background and grab keystrokes?

thanks in advance,
Shachar

Use the events thing, for example:

import bge
from bge import logic
from bge import events

cont = logic.getCurrentController()

key = logic.keyboard.events

#and for keyboard buttons

Dkey(or whatever variable you want) = key[events.DKEY]

more info can be found here http://www.blender.org/documentation/blender_python_api_2_58_release/bge.events.html

I am getting an import error - “ImportError: No module named bge” (I am using blender 2.49)

2.49 huh? The python changed for 2.4 and 2.5. I gave you code to 2.5. I don’t know if you can even use the events module for 2.4.

You should switch to 2.5, you won’t regret it! :stuck_out_tongue:

It might be possible to do in 2.49, but I don’t recall, either. Take a look at the Python API and see if you can spot it. However, I, too, recommend going to 2.5.

The problem with that idea is that unfortunately the rest of my code, and all of the previous scripts for the project are already in 2.49…
|sigh|

ok, so i transfered the code to 2.5, and am trying to work with it, but the original problem returned - it is still missing some of the clicks sent to the keyboard by the response box as keystrokes, unlike python commands like “raw_input()” which catch all of the keystrokes, but don’t run in the background…
(just to clarify, this works well with the regular keyboard - but has the same problem as the “keyboard” logic block, and i assume it’s because they work with the same mechanism under the hood)

any other ideas besides using the 2.5 keyboard event?

(i think the problem may be in a mismatch between the sampling rate of the keyboard event queue - is there any way i can tamper with that?)

thanks in advance,
Sh

Wrong thread :stuck_out_tongue:

  1. There’s a keyboard target variable in the Keyboard Sensor that will log every key (unless that’s the problem you’re having). Is that what you need?
  2. Are you running the keyboard sensor / activating your script every frame?

1 - thats exactly the problem i have. it isn’t logging every key - it’s missing some

2 - I’m activating via an “always” logic block set to pulse mode. is there a better way? what do you meen by “in every frame”?

I think he meant the true pulse button on your always.

Than yes, thats what i am doing… i’m using the true pulse button, and the always is set to 0 delay (though iv’e tried adding a delay and fidgeting with the other buttons but that didn’t help)

You could try using a Keyboard-All Keys sensor instead of the Always (true pulse) sensor. That way the script only runs when you need it to, instead of wasting resources when you don’t need it to.

Could you please say again what you’re trying to achieve?

GameKeys is the keycode api…

@agoose77 - Not in 2.5 it isn’t - it’s bge.events.

I don’t know if this will help, because I don’t know too much about this, but I’ve noticed when I test the game I’m working on, there are certain key combinations that just don’t work. For example, If I have a character running while aiming a gun and shooting, you would hold down up, A, and press S to shoot (that’s the way I set it up on mine anyway) but I noticed if you are holding down up, A, and S, and then you press left he won’t turn. Also, if your holding down up, left, A and press S he won’t shoot. For some reason that combination of keys doesn’t work. So maybe that’s the problem your having. Like I said I don’t know TOO much about this, but it’s just a thought that might be helpful.

I had a similar problem with pressing a few of the Arrow keys + Shift + Space, and apparently my HP keyboard can’t handle that combination of keys because of its type. Perhaps it’s just a keyboard or driver error.