Skip to content

Commit

Permalink
Fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam777Z committed Aug 19, 2021
1 parent 4788f56 commit 0f61ba2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 82 deletions.
67 changes: 42 additions & 25 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
; Map the Share button on the controller

; Decimal key code, see the bottom of this file for a list
; Multiple keys are supported. Separate multiple keys with commas. Example: 1,2,3
; Multiple keys are supported. Separate multiple keys with commas.
; If you set it to 0, set longpress_key correctly, and set hold_mode = 2,
; tapping the button for less than longpress_duration will do nothing.
share_key = 1
; Default: 91,56,84 (Xbox Game Bar - Take screenshot: WINDOWS+ALT+PRINT SCREEN)
share_key = 91,56,84

; If 1, "key" will be sent for as long as button is pressed and longpress_*
; will be ignored - simple as that.
Expand All @@ -17,11 +18,13 @@ share_key = 1
; is held for longer than that.
; Note that even after the button has been held for longer than longpress_duration,
; you still need to release it before anything will be sent.
share_hold_mode = 1
; Default: 2
share_hold_mode = 2

; See above.
; Multiple keys are supported. Separate multiple keys with commas. Example: 1,2,3
share_longpress_key = 1
; Multiple keys are supported. Separate multiple keys with commas.
; Default: 91,56,19 (Xbox Game Bar - Start/stop recording: WINDOWS+ALT+R)
share_longpress_key = 91,56,19

; See above. In milliseconds (1000 = 1 second)
share_longpress_duration = 1000
Expand All @@ -45,8 +48,8 @@ share_duration = 1

; See above.

; Default: 255 (Xbox Game Bar)
xbox_key = 255
; Default: 91,34 (Open Xbox Game Bar: WINDOWS+G)
xbox_key = 91,34

xbox_hold_mode = 1
xbox_longpress_key = 1
Expand All @@ -58,41 +61,41 @@ xbox_duration = 1
; Settings for other controllers follow

[controller2]
share_key = 1
share_hold_mode = 1
share_longpress_key = 1
share_key = 91,56,84
share_hold_mode = 2
share_longpress_key = 91,56,19
share_longpress_duration = 1000
share_delay = 0
share_duration = 1
xbox_key = 255
xbox_key = 91,34
xbox_hold_mode = 1
xbox_longpress_key = 1
xbox_longpress_duration = 1000
xbox_delay = 0
xbox_duration = 1

[controller3]
share_key = 1
share_hold_mode = 1
share_longpress_key = 1
share_key = 91,56,84
share_hold_mode = 2
share_longpress_key = 91,56,19
share_longpress_duration = 1000
share_delay = 0
share_duration = 1
xbox_key = 255
xbox_key = 91,34
xbox_hold_mode = 1
xbox_longpress_key = 1
xbox_longpress_duration = 1000
xbox_delay = 0
xbox_duration = 1

[controller4]
share_key = 1
share_hold_mode = 1
share_longpress_key = 1
share_key = 91,56,84
share_hold_mode = 2
share_longpress_key = 91,56,19
share_longpress_duration = 1000
share_delay = 0
share_duration = 1
xbox_key = 255
xbox_key = 91,34
xbox_hold_mode = 1
xbox_longpress_key = 1
xbox_longpress_duration = 1000
Expand Down Expand Up @@ -131,7 +134,7 @@ xbox_duration = 1
; 26 [ or {
; 27 ] or }
; 28 ENTER
; 29 CTRL
; 29 LEFT CONTROL
; 30 A
; 31 S
; 32 D
Expand All @@ -157,8 +160,8 @@ xbox_duration = 1
; 52 . or >
; 53 / or ?
; 54 RIGHT SHIFT
; 55 * or PRINT SCREEN
; 56 ALT
; 55 *
; 56 LEFT ALT
; 57 SPACEBAR
; 58 CAPS LOCK
; 59 F1
Expand All @@ -184,11 +187,25 @@ xbox_duration = 1
; 79 1 or END
; 80 2 or DOWN
; 81 3 or PAGE DOWN
; 82 0 or INS
; 83 . or DEL
; 82 0 or INSERT
; 83 . or DELETE
; 84 PRINT SCREEN
; 87 F11
; 88 F12
; 91 LEFT WINDOWS
; 92 RIGHT WINDOWS
; 93 MENU
; 255 Xbox Game Bar (LEFT WINDOWS+G)
; 98 ZOOM
; 99 HELP
; 100 F13
; 101 F14
; 102 F15
; 103 F16
; 104 F17
; 105 F18
; 106 F19
; 107 F20
; 108 F21
; 109 F22
; 110 F23
; 118 F24
127 changes: 74 additions & 53 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ std::string GetExePath()
}

#define MAX_CONTROLLERS 4
#define XBOX_GAME_BAR_KEY 255

static int num_controllers = 0;
static SDL_GameController* controllers[MAX_CONTROLLERS];
Expand Down Expand Up @@ -160,50 +159,49 @@ void fatal_error(const std::string&& text)
exit(1);
}

void open_xbox_game_bar()
bool is_open_xbox_game_bar_keys(std::vector<int> code)
{
INPUT inp[4];

// Press LEFT WINDOWS key
inp[0].type = INPUT_KEYBOARD;
inp[0].ki.wScan = 91; // hardware scan code for LEFT WINDOWS key
inp[0].ki.wVk = 91; // virtual-key code for LEFT WINDOWS key
inp[0].ki.time = 0;
inp[0].ki.dwExtraInfo = 0;
inp[0].ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_SCANCODE; // 0 for key press

// Press G key
inp[1] = inp[0];
inp[1].ki.wScan = 34; // hardware scan code for G key
inp[1].ki.wVk = 0; // virtual-key code, we're doing scan codes instead
inp[1].ki.dwFlags = KEYEVENTF_SCANCODE; // 0 for key press

// Release LEFT WINDOWS key
inp[2] = inp[0];
//inp[2].ki.wScan = 91; // hardware scan code for LEFT WINDOWS key
//inp[2].ki.wVk = 91; // virtual-key code for LEFT WINDOWS key
inp[2].ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // 0 for key press

// Release G key
inp[3] = inp[1];
//inp[3].ki.wScan = 34; // hardware scan code for G key
//inp[3].ki.wVk = 0; // virtual-key code, we're doing scan codes instead
inp[3].ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // 0 for key press

SendInput(4, inp, sizeof(INPUT));

//SDL_Log("Opened Xbox Game Bar.\n\n");
bool key1 = false;
bool key2 = false;

if (code.size() == 2)
{
for (std::size_t i = 0; i < code.size(); ++i)
{
if (code[i] == 91 || code[i] == 92)
{
key1 = true;
}
else if (code[i] == 34)
{
key2 = true;
}
}
}

return key1 && key2;
}

void key_down(int code)
{
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wScan = code; // hardware scan code for key
inp.ki.wVk = 0; // virtual-key code, we're doing scan codes instead

if (code == 91 || code == 92 || code == 93)
{
inp.ki.wVk = code; // virtual-key code
inp.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_SCANCODE; // 0 for key press
}
else
{
inp.ki.wVk = 0; // virtual-key code, we're doing scan codes instead
inp.ki.dwFlags = KEYEVENTF_SCANCODE; // 0 for key press
}

inp.ki.time = 0;
inp.ki.dwExtraInfo = 0;
inp.ki.dwFlags = KEYEVENTF_SCANCODE; // 0 for key press

SendInput(1, &inp, sizeof(INPUT));
}

Expand All @@ -212,35 +210,58 @@ void key_up(int code)
INPUT inp;
inp.type = INPUT_KEYBOARD;
inp.ki.wScan = code; // hardware scan code for key
inp.ki.wVk = 0; // virtual-key code, we're doing scan codes instead

if (code == 91 || code == 92 || code == 93)
{
inp.ki.wVk = code; // virtual-key code
inp.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // 0 for key press
}
else
{
inp.ki.wVk = 0; // virtual-key code, we're doing scan codes instead
inp.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // 0 for key press
}

inp.ki.time = 0;
inp.ki.dwExtraInfo = 0;
inp.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP; // 0 for key press

SendInput(1, &inp, sizeof(INPUT));
}

void key_tap(std::vector<int> code, int64_t delay, int64_t duration)
{
std::this_thread::sleep_for(std::chrono::milliseconds(delay));

for (std::size_t i = 0; i < code.size(); ++i)
bool open_xbox_game_bar_keys = is_open_xbox_game_bar_keys(code);

// Open Xbox Game Bar on button release only
if (!open_xbox_game_bar_keys)
{
// Open Xbox Game Bar on button release only
if (code[i] != XBOX_GAME_BAR_KEY)
for (std::size_t i = 0; i < code.size(); ++i)
{
key_down(code[i]);
}
}

std::this_thread::sleep_for(std::chrono::milliseconds(duration));

for (std::size_t i = 0; i < code.size(); ++i)
if (open_xbox_game_bar_keys)
{
if (code[i] == XBOX_GAME_BAR_KEY)
for (std::size_t i = 0; i < code.size(); ++i)
{
key_down(code[i]);
}

for (std::size_t i = 0; i < code.size(); ++i)
{
open_xbox_game_bar();
key_up(code[i]);
}
else

//SDL_Log("Opened Xbox Game Bar.\n");
}
else
{
for (std::size_t i = 0; i < code.size(); ++i)
{
key_up(code[i]);
}
Expand Down Expand Up @@ -488,10 +509,10 @@ int WINAPI WinMain(_In_ HINSTANCE hThisInstance, _In_opt_ HINSTANCE hPrevInstanc
{
if (settings.key.size() != 0)
{
for (std::size_t k = 0; k < settings.key.size(); ++k)
// Open Xbox Game Bar on button release only
if (!is_open_xbox_game_bar_keys(settings.key))
{
// Open Xbox Game Bar on button release only
if (settings.key[k] != XBOX_GAME_BAR_KEY)
for (std::size_t k = 0; k < settings.key.size(); ++k)
{
key_down(settings.key[k]);
}
Expand All @@ -513,13 +534,13 @@ int WINAPI WinMain(_In_ HINSTANCE hThisInstance, _In_opt_ HINSTANCE hPrevInstanc
{
if (settings.key.size() != 0)
{
for (std::size_t k = 0; k < settings.key.size(); ++k)
if (is_open_xbox_game_bar_keys(settings.key))
{
if (settings.key[k] == XBOX_GAME_BAR_KEY)
{
open_xbox_game_bar();
}
else
key_tap(settings.key, 0, 1);
}
else
{
for (std::size_t k = 0; k < settings.key.size(); ++k)
{
key_up(settings.key[k]);
}
Expand Down
8 changes: 4 additions & 4 deletions resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ IDI_ICON1 ICON "XboxOneLogo.ico"
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,9,0
PRODUCTVERSION 1,0,9,0
FILEVERSION 1,1,0,0
PRODUCTVERSION 1,1,0,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -78,10 +78,10 @@ BEGIN
BLOCK "040904b0"
BEGIN
VALUE "FileDescription", "Xbox Controller button remapper"
VALUE "FileVersion", "1.0.9.0"
VALUE "FileVersion", "1.1.0.0"
VALUE "LegalCopyright", "Copyright (C) 2015-2021"
VALUE "ProductName", "Xbox Controller button remapper"
VALUE "ProductVersion", "1.0.9.0"
VALUE "ProductVersion", "1.1.0.0"
END
END
BLOCK "VarFileInfo"
Expand Down

0 comments on commit 0f61ba2

Please sign in to comment.