Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuzu-Typ committed Oct 20, 2024
0 parents commit bf4b313
Show file tree
Hide file tree
Showing 8 changed files with 534 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.j2k
!default.j2k
17 changes: 17 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Copyright (C) 2024 Zuzu_Typ

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# JoyToKey - Map your Xbox controller to keyboard keys
With JoyToKey you can create custom mappings of Xbox keys with ease.

**Windows only**

## Setup
You need to install XInput-Python and winput to use JoyToKey:
```
pip install -r requirements.txt
```

## Create a mapping
Run `configure.py` to create a new mapping.

It will allow you to create a new mapping by first pressing a **button**, pressing a **trigger** or moving a **stick** on your Xbox controller, followed by a corresponding keyboard key.

Currently mouse movement and mouse buttons cannot be used.

## Run a mapping
When you've created a mapping that you want to use, simply run:
```
python joytokey.py "<your mapping name>.j2k"
```

Sample output:
```
Mapping started with config:
A is mapped to VK_E
B is mapped to VK_Q
LEFT_STICK UP is mapped to VK_W
LEFT_STICK DOWN is mapped to VK_S
LEFT_STICK LEFT is mapped to VK_A
LEFT_STICK RIGHT is mapped to VK_D
Press CTRL + C to stop.
```

If you overwrite the mapping called "default", you can omit the mapping file parameter:
```
python joytokey.py
```
53 changes: 53 additions & 0 deletions configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from j2klib import JoyToKeyConfig, JoyToKeyCreator, JoyMapper

import os

j2kc = JoyToKeyCreator()

print("Welcome to the JoyToKey configurator.")

mappings = []

while True:
add_new_key = input("Would you like to add a new key? ([Y]/N): ")
add_new_key = add_new_key or "Y"

if add_new_key.upper() not in ("Y", "N"):
print("Invalid input. Please enter Y for yes or N for no.")
continue

if add_new_key.upper() == "N":
break

print("Please press the key on your Xbox controller now.", end="\r")

xkey = j2kc.listen_for_xinput()

print("Now press the key on your keyboard you want to map it to.", end="\r")

wkey = j2kc.listen_for_winput()

xkey["key"] = wkey

mapping = JoyMapper.from_dict(xkey)

mappings.append(mapping)

print(" " * 60, end="\r")

print(mapping.explain())
print()

cfg = JoyToKeyConfig(mappings)

print("Please name your new mapping.")
print()
mapping_name = input("name: ")

file_name = f"{mapping_name}.j2k"

cfg.to_file(file_name)

print(f"Mapping saved as {file_name}")

os.system("pause")
51 changes: 51 additions & 0 deletions default.j2k
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"version": "0.1.0",
"mappers": [
{
"type": "button",
"button": 4096,
"key": 69
},
{
"type": "button",
"button": 8192,
"key": 81
},
{
"type": "stick",
"stick": 0,
"direction": [
0,
1
],
"key": 87
},
{
"type": "stick",
"stick": 0,
"direction": [
0,
-1
],
"key": 83
},
{
"type": "stick",
"stick": 0,
"direction": [
-1,
0
],
"key": 65
},
{
"type": "stick",
"stick": 0,
"direction": [
1,
0
],
"key": 68
}
]
}
Loading

0 comments on commit bf4b313

Please sign in to comment.