Disable keyboard input when controller is plugged in.

I have a small issue for my player controller, when I have a keyboard without controller it runs the system fine.
But when I try to implement the controller(for now just plugged it in) everything stops and I found that is because the keyboard is spamming the Idle state… So now the question in the title how can I see if a controller is plugged in or not?

Try adding a game property to your object reading the keyboard. Make it a Boolean and call it ‘joystickPresent’

Then add an ‘always’ sensor and tie it to a python controller. Create a new script and use this code:


import bge

controller = bge.logic.getCurrentController()
owner = controller.owner

owner['joystickPresent'] = len(bge.logic.joysticks) > 0

If you want to suppress the keyboard input when the joystick is plugged in, then add a property sensor tied to ‘joystickPresent’ so that the keyboard controller only fires if they keyboard is idle and the ‘joystickPresent’ is false.

This might cure the symptom but not the cause. I think the problem is somewhere completely different. Without more details it is hard to guess. My keyboard does not spam ;).

What sort of controller are you talking about. How is it connected with your keyboard? How do you evaluate the keyboard input? How do you evaluate the controller input? etc.

The issue is that when I plug in the xbox controller (it works and I already can use it for movement) the keyboard keeps the player on Idle, logically because Idle is active when no input is coming from the keyboard. For the processing I and a few friends made a controllerProcessing script where every input into a list called the event list. from there we keep the first and second item in the list and use that for the commands to change state. so we have an processing def that keeps appending the Idle to the list.

So basically you want to go with whatever input is giving true pulses (either the keyboard or the controller). I actually made a module for controller mapping as well as dealing with wanting a single object to be driven by possibly different input sources (i.e. a controller and/or the keyboard). It’s called BGInput. I’ve just added mouse button clicking and fixed a bug with using the mouse axes. The example for it shows a pretty good amount of the different features. You can find it on my Google Code page here. To download it and the example, you’ll probably need to use a SVN client like TortoiseSVN.

I already have a good system working in the input. But the issue is more of how can I see if a gamecontroller plugged into the usb or not?

This sounds more like it is idle because there is nothing going on ;).
Indeed this sounds logical.

.

I guess you mean you have a Python controller running that constantly checks the keyboard input?

Why you check only two?

I guess you use bge.logic.keyboard.events. This gives you the status of ALL keys.
Better use the keyboard sensor. It gives you the events of just pressed, hold and just released keys (= active and changes keystates). If there is no key pressed you get an empty list.

As a great benefit you let the keyboard sensor sense when a key event happens rather than constantly run Python code. This is pretty efficient.

thanks for all the suggestions, but to be correct monster your second point is more that the controllerProcessing script manages all the input, instead separate. further the list workings is the first item is the state you have, the second item is the state it goes towards if it is allowed. that is managed by an allowedList. for the last one one it is a bit more complex, I tried that but then the smoothing wouldn’t work.

I do not know your xbox controller code. So I can’t help. I know about how the keyboard sensor works but I do not know your code that works with that. So I can’t help here either :(.

isn’t there a function I can call to see if there is a device attached to the computer?

You can look for some Python libraries.