detect button press and use it in a Python script?
#1
hi guys, I need help. How do I detect a button press (of my Xbox Controller) in Python?

I need to use it as part of an if-condition. Basically this:

"if button b is pressed then ..."

Any ideas?
Thanks guys!
Reply
#2
you could map a key to run a script
https://kodi.wiki/view/Keymap#Add-on_built-in.27s
Reply
#3
I have already mapped my buttons. In this case it' not an option though.

I actually need the Python code which detects a button press. 

Like I described above I need it as an if-condition.

What does the Python code look like for: "if button b is pressed"?
Reply
#4
You could create a service addon that listens to all the incoming notifications and does something if the notification is about a key press (and specifically the correct key press).

Here's a little bit about service addons:

https://kodi.wiki/view/Service_add-ons

And specifically the onNotification hook:

https://codedocs.xyz/xbmc/xbmc/group__py...7a8a9a8dc7

I don't remember off hand what notification event you need to listen for, so you might need to just create the service addon and have it log the messages to see if you can figure out which one will correspond to a keypress.

One last option. I've never used it, but there is an addon called Kodi Callbacks that might let you configure an event using a keypress as a trigger.

https://kodi.wiki/view/Add-on:Kodi_Callbacks
Reply
#5
thank you! I need more help though. I tried the Kodi Callback add-on. (I knew it before and used it for other stuff.) It's not of any use for regarding pressing buttons.

I am still pretty much clueless what to do.
Reply
#6
perhaps you can use the python inputs library: https://inputs.readthedocs.io/en/latest/index.html
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#7
I created a service add-on and used python inputs library. Finally it's working.

Appreciate your help!!
Reply
#8
Okay, it's working in Windows, but not on Android.

there is definitely something in the log, but I don't know how to capture this in python. Help please?

Code:
2020-08-03 22:42:36.029 T:9996    DEBUG <general>: BUTTON [ 0 ] on "Xbox 360-compatible controller" pressed
2020-08-03 22:42:36.029 T:9996    DEBUG <general>: FEATURE [ a ] on game.controller.default pressed (ignored)
2020-08-03 22:42:36.029 T:9996    DEBUG <general>: FEATURE [ a ] on game.controller.default pressed (ignored)
2020-08-03 22:42:36.145 T:9996    DEBUG <general>: BUTTON [ 0 ] on "Xbox 360-compatible controller" released
2020-08-03 22:42:36.145 T:9996    DEBUG <general>: FEATURE [ a ] on game.controller.default released
2020-08-03 22:42:36.145 T:9996    DEBUG <general>: FEATURE [ a ] on game.controller.default released
Reply
#9
@Roman_V_M 

Would you have an idea to my problem?

Thank you!!
Reply
#10
Let me guys be more specific what it is I am after:

I'm pressing RB (right bumper) on my Xbox controller and - in Windows - I am able to toggle between a handful of different listening modes dedicated specifically to music. These come from my Pioneer AV-Receiver. Pretty handy when using the terrific Kodi YouTube add-on. The button press is captured by the external python library.

However, the library doesn’t recognize my Xbox Controller on the Shield 2019 Pro. Not wireless anyways.

Something similar already exists with AspectRatio in Kodi:

https://kodi.wiki/view/Action_IDs <rightthumb>AspectRatio</rightthumb>

We can all toggle through various aspect ratio modes when pressing the right thumb when in full screen mode. It’s pretty much what I need, only with a different action. Hopefully someone has an idea how I can achieve this without the need of an external python library.

​​​​​​​Thank you!
Reply
#11
(2020-08-09, 20:41)3000 Wrote: I'm pressing RB (right bumper) on my Xbox controller

what action is mapped to this button?

if you map it to RunScript() (as suggested in the first reply) you can have your addon execute this action + whatever code you need to toggle those listening modes on your receiver.
just a thought...
Do not PM or e-mail Team-Kodi members directly asking for support.
Always read the Forum rules, Kodi online-manual, FAQ, Help and Search the forum before posting.
Reply
#12
I've never worked with Python inputs library. From a Kodi addon perspective, an addon works with Kodi actions that can be mapped do different physical controller events, e.g. keypresses, touchscreen gestures etc. So I have no idea how to intercept actual controller events from an addon.
Reply
#13
ok thanks.
Reply
#14
(2020-08-10, 01:56)ronie Wrote:
(2020-08-09, 20:41)3000 Wrote: I'm pressing RB (right bumper) on my Xbox controller

what action is mapped to this button?

if you map it to RunScript() (as suggested in the first reply) you can have your addon execute this action + whatever code you need to toggle those listening modes on your receiver.
just a thought...
The action is the script I created. It uses the python inputs library as you suggested. It recognizes my button press as an event - each time I press the button. I am using an infinite loop (itertools.cycle) to toggle between selected modes. Works as it should in Windows.

Different story on Android. 
The script fails because the library doesn't recognize my Xbox Wireless Controller(s) on the Shield.  

So it's not about using keymaps.xml. The problem is the failed recognition of the button press (or any key press for that matter) within Kodi. 

I think it's definitely possible to capture the button/key press.
Reply
#15
no rant, just a simple realisation:

NOT one Team-Kodi Member was able to answer the simple question how to natively capture a button press. 

Wow.

Mark the thread as solved, I guess.
Reply



Logout Mark Read Team Forum Stats Members Help
detect button press and use it in a Python script?0
This forum uses Lukasz Tkacz MyBB addons.