Skip to content

Commit c76b5b6

Browse files
committed
Support for deadzone customization
1 parent cb31517 commit c76b5b6

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ It can be inmported like this:
1717
XInput\-Python provides a few functions:
1818
`get_connected() -> (bool, bool, bool, bool)` Query which controllers are connected \(note: don't query each frame\)
1919

20-
`get_state(user_index) -> State` Get the State of the controller `user_index`
20+
`get_state(user_index) -> State` Gets the State of the controller `user_index`
2121

2222
`get_button_values(state) -> dict` Returns a dictionary, showing which buttons are currently being pressed\.
2323

@@ -29,6 +29,15 @@ XInput\-Python provides a few functions:
2929

3030
`get_battery_information(user_index) -> (<type>, <level>)` Returns the battery information for `user_index`
3131

32+
`set_deadzone(deadzone, value) -> None` Sets the deadzone values for left/right thumb stick and triggers\.
33+
34+
The following deadzones exist:
35+
`XInput.DEADZONE_LEFT_THUMB` \- \(range 0 to 32767\) Left thumb stick deadzone \(default is 7849\)
36+
37+
`XInput.DEADZONE_RIGHT_THUMB` \- \(range 0 to 32767\) Right thumb stick deadzone \(default is 8689\)
38+
39+
`XInput.DEADZONE_TRIGGER` \- \(range 0 to 255\) Trigger deadzone \(default is 30\)
40+
3241
#### Using Events
3342
You can also use the Event\-system:
3443

README.rml

+10-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ It can be inmported like this:
1313
XInput-Python provides a few functions:
1414
[code]get_connected() -> (bool, bool, bool, bool)[/code] Query which controllers are connected (note: don't query each frame)
1515

16-
[code]get_state(user_index) -> State[/code] Get the State of the controller [code]user_index[/code]
16+
[code]get_state(user_index) -> State[/code] Gets the State of the controller [code]user_index[/code]
1717

1818
[code]get_button_values(state) -> dict[/code] Returns a dictionary, showing which buttons are currently being pressed.
1919

@@ -25,6 +25,15 @@ XInput-Python provides a few functions:
2525

2626
[code]get_battery_information(user_index) -> (<type>, <level>)[/] Returns the battery information for [code]user_index[/]
2727

28+
[code]set_deadzone(deadzone, value) -> None[/] Sets the deadzone values for left/right thumb stick and triggers.
29+
30+
The following deadzones exist:
31+
[code]XInput.DEADZONE_LEFT_THUMB[/] - (range 0 to 32767) Left thumb stick deadzone (default is 7849)
32+
33+
[code]XInput.DEADZONE_RIGHT_THUMB[/] - (range 0 to 32767) Right thumb stick deadzone (default is 8689)
34+
35+
[code]XInput.DEADZONE_TRIGGER[/] - (range 0 to 255) Trigger deadzone (default is 30)
36+
2837
[s3]Using Events[/]
2938
You can also use the Event-system:
3039
[code]events = get_events()[/code]

README.rst

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Using XInput\-Python
3737
| XInput\-Python provides a few functions\:
3838
| :code:`get_connected() -> (bool, bool, bool, bool)` Query which controllers are connected \(note\: don\'t query each frame\)
3939
|
40-
| :code:`get_state(user_index) -> State` Get the State of the controller :code:`user_index`
40+
| :code:`get_state(user_index) -> State` Gets the State of the controller :code:`user_index`
4141
|
4242
| :code:`get_button_values(state) -> dict` Returns a dictionary\, showing which buttons are currently being pressed\.
4343
|
@@ -49,6 +49,15 @@ Using XInput\-Python
4949
|
5050
| :code:`get_battery_information(user_index) -> (<type>, <level>)` Returns the battery information for :code:`user_index`
5151
|
52+
| :code:`set_deadzone(deadzone, value) -> None` Sets the deadzone values for left\/right thumb stick and triggers\.
53+
|
54+
| The following deadzones exist\:
55+
| :code:`XInput.DEADZONE_LEFT_THUMB` \- \(range 0 to 32767\) Left thumb stick deadzone \(default is 7849\)
56+
|
57+
| :code:`XInput.DEADZONE_RIGHT_THUMB` \- \(range 0 to 32767\) Right thumb stick deadzone \(default is 8689\)
58+
|
59+
| :code:`XInput.DEADZONE_TRIGGER` \- \(range 0 to 255\) Trigger deadzone \(default is 30\)
60+
|
5261
5362
Using Events
5463
^^^^^^^^^^^^

XInput.py

+51-2
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,56 @@ def XInputGetBatteryInformation(dwUserIndex, devType, batteryInformation):
114114

115115
_last_checked = 0
116116

117+
DEADZONE_LEFT_THUMB = 0
118+
DEADZONE_RIGHT_THUMB = 1
119+
DEADZONE_TRIGGER = 2
120+
121+
DEADZONE_DEFAULT = -1
122+
123+
_deadzones = [{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
124+
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
125+
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD},
126+
{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
127+
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
128+
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD},
129+
{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
130+
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
131+
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD},
132+
{DEADZONE_RIGHT_THUMB : XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE,
133+
DEADZONE_LEFT_THUMB : XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE,
134+
DEADZONE_TRIGGER : XINPUT_GAMEPAD_TRIGGER_THRESHOLD}]
135+
117136
class XInputNotConnectedError(Exception):
118137
pass
119138

120139
class XInputBadArgumentError(ValueError):
121140
pass
122141

142+
def set_deadzone(dzone, value):
143+
global XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE, XINPUT_GAMEPAD_TRIGGER_THRESHOLD
144+
145+
assert dzone >= 0 and dzone <= 2, "invalid deadzone"
146+
147+
if value == DEADZONE_DEFAULT:
148+
value = 7849 if dzone == DEADZONE_LEFT_THUMB else \
149+
8689 if dzone == DEADZONE_RIGHT_THUMB else \
150+
30
151+
152+
if dzone == DEADZONE_LEFT_THUMB:
153+
assert value >= 0 and value <= 32767
154+
if value == DEADZONE_DEFAULT: XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = 7849
155+
else: XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE = value
156+
157+
elif dzone == DEADZONE_RIGHT_THUMB:
158+
assert value >= 0 and value <= 32767
159+
if value == DEADZONE_DEFAULT: XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = 8689
160+
else: XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE = value
161+
162+
else:
163+
assert value >= 0 and value <= 255
164+
if value == DEADZONE_DEFAULT: XINPUT_GAMEPAD_TRIGGER_THRESHOLD = 30
165+
else: XINPUT_GAMEPAD_TRIGGER_THRESHOLD = value
166+
123167
def get_connected():
124168
state = XINPUT_STATE()
125169
out = [False] * 4
@@ -424,6 +468,8 @@ def get_events():
424468
root.title("XInput")
425469
canvas = tk.Canvas(root, width= 600, height = 400, bg="white")
426470
canvas.pack()
471+
472+
set_deadzone(DEADZONE_TRIGGER,10)
427473

428474
class Controller:
429475
def __init__(self, center):
@@ -613,6 +659,9 @@ def __init__(self, center):
613659
canvas.itemconfig(controller.Y_button, fill="")
614660
elif event.button == "X":
615661
canvas.itemconfig(controller.X_button, fill="")
616-
617-
root.update()
662+
663+
try:
664+
root.update()
665+
except tk.TclError:
666+
break
618667

0 commit comments

Comments
 (0)