t2:gpc_scripting:usb_hid

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
t2:gpc_scripting:usb_hid [2021/01/20 05:58]
scachi [Anti Recoil]
t2:gpc_scripting:usb_hid [2024/04/07 03:00] (current)
Scachi [GPC functions]
Line 1: Line 1:
 ====== USB HID ====== ====== USB HID ======
-... WORK IN PROGRESS ... 
  
 The Output Protocol "USB Multi Interface HID" is a bit special.\\ The Output Protocol "USB Multi Interface HID" is a bit special.\\
Line 23: Line 22:
  
 When you want to forward mouse input or keyboard input use the corresponding _passthru function.\\ ​ When you want to forward mouse input or keyboard input use the corresponding _passthru function.\\ ​
-As soon as you want to alter the input before outputting it you need to be careful: + 
-||Combination of mouse_passthru() with mouse_set() : The first function ​will overwrite the output buffer with the latest data received from the connected ​mouse||+**As soon as you want to alter the input before outputting it you need to be careful:** 
 +|| The last value set for a mouse filed is kept between main() iterations. Unless mouse_passthru() is called, which will overwrite the value set by the script.|| 
 +**Meaning:​** 
 +||If you use mouse_set() function to alter the mouse status (moving mouse/ pressing buttons) you need to always set all inputs each time (x,​y,​button,​wheels) for the mouse to get no unwanted mouse movements/​states. ​||
 More information:​\\ More information:​\\
 [[https://​github.com/​J2Kbr/​TitanTwo/​issues/​232|mouse_passthru and mouse_set]] \\ [[https://​github.com/​J2Kbr/​TitanTwo/​issues/​232|mouse_passthru and mouse_set]] \\
Line 52: Line 54:
 // note: when a value of 1 is still to strong anti recoil try a higher value for mouse_speed // note: when a value of 1 is still to strong anti recoil try a higher value for mouse_speed
 const int mouse_speed = 10; // <- should be around 10 or higher, a value too low can stop the mouse from working const int mouse_speed = 10; // <- should be around 10 or higher, a value too low can stop the mouse from working
-const int AntiRecoil_X = 0; // anti recoil horizontal +int AntiRecoil_X = 0; // anti recoil horizontal 
-const int AntiRecoil_Y = 1; // anti recoil vertical+int AntiRecoil_Y = 1; // anti recoil vertical
  
    
Line 97: Line 99:
    
 // configuration ​ // configuration ​
-const fix32 antirecoil_X = 0.0; // anti recoil horizontal +fix32 antirecoil_X = 0.0; // anti recoil horizontal 
-const fix32 antirecoil_Y = 0.5; // anti recoil vertical+fix32 antirecoil_Y = 0.5; // anti recoil vertical
  
 // advanced configuration // advanced configuration
Line 136: Line 138:
  
             if (antirecoil_X < 0.0) RX += (int)ceil((ARX));​             if (antirecoil_X < 0.0) RX += (int)ceil((ARX));​
-            else RX += (int)floor((ARX));​+            else if (antirecoil_X > 0.0) RX += (int)floor((ARX));​
             ​             ​
             if (antirecoil_Y < 0.0) RY += (int)ceil((ARY));​             if (antirecoil_Y < 0.0) RY += (int)ceil((ARY));​
-            else RY += (int)floor((ARY));​+            else if (antirecoil_Y > 0.0) RY += (int)floor((ARY));​
             ​             ​
             mouse_set(MOUSE_X,​ X+RX);             mouse_set(MOUSE_X,​ X+RX);
Line 154: Line 156:
  
 </​code>​ </​code>​
-... WORK IN PROGRESS ​...+ 
 +==== Controller to Keyboard ==== 
 +Mapping controller input to keyboard HID output. 
 +=== Basic version === 
 +<code gpc2> 
 +#include <​keyboard.gph> 
 +#include <mouse.gph> 
 +  
 +init { 
 +    keymapping();​ 
 +    mousemapping();​ 
 +
 +main { 
 +    key_passthru();​ 
 +    if(mouse_status(MREPORT_UPDATED)) { 
 +        mouse_passthru();​ 
 +    } 
 +  
 +    // dpad up mapping to key E 
 +    if(is_active(BUTTON_10)) key_set(KEY_E,​ TRUE); 
 +
 +</​code>​ 
 + 
 +=== Extended version === 
 +To make sure the key is released on release of the button as using KMG capture the key may not be reported as released automatically. 
 +<code gpc2> 
 +#include <​keyboard.gph> 
 +#include <mouse.gph> 
 +  
 +init { 
 +    keymapping();​ 
 +    mousemapping();​ 
 +
 +main { 
 +    key_passthru();​ 
 +    if(mouse_status(MREPORT_UPDATED)) { 
 +        mouse_passthru();​ 
 +    } 
 +  
 +    // dpad up mapping to key E 
 +    if(is_active(BUTTON_10)) {  
 +        keySet(KEY_E,​ TRUE); // press 
 +    } 
 +    if (event_release(BUTTON_10)) { 
 +        keySet(KEY_E,​ FALSE); // release 
 +    } 
 +
 + 
 +void keySet(uint8 key, bool flag) { 
 +    if (key_get(key) != flag) { 
 +        key_set(key,​ flag); 
 +    } 
 +
 +</​code>​ 
 + 
 +==== Keyboard to Keyboard ==== 
 +Mapping keyboard input to keyboard HID output. 
 +Useful for some games where you can't change the key assigned to actions. 
 +<code gpc2> 
 +#include <​keyboard.gph>​ 
 +#include <​mouse.gph>​ 
 + 
 +init { 
 +    port_connect(PORT_USB_C,​ PROTOCOL_HID);​ 
 +    keymapping();​ 
 +    mousemapping();​ 
 +
 + 
 + 
 +main { 
 +    key_passthru();​ 
 +    if(mouse_status(MREPORT_UPDATED)) { 
 +        mouse_passthru();​ 
 +    } 
 + 
 +    // shift <-> ctrl 
 +    keyMap(KEY_LEFTSHIFT,​ KEY_LEFTCONTROL);​ 
 +    keyMap(KEY_LEFTCONTROL,​ KEY_LEFTSHIFT,​ FALSE, KEY_LEFTSHIFT);​ // <-- for circular mapping 
 + 
 +    // additional shift mapping : alt -> shift 
 +    keyMap(KEY_LEFTALT,​ KEY_LEFTSHIFT,​ TRUE); // <-- for additional key to same destination 
 + 
 +    // caps -> alt 
 +    keyMap(KEY_CAPSLOCK,​ KEY_LEFTALT);​ 
 + 
 +    // left-> page down 
 +    keyMap(KEY_LEFTARROW,​ KEY_PAGEDOWN);​ 
 + 
 +    // right-> page up 
 +    keyMap(KEY_RIGHTARROW,​ KEY_PAGEUP);​ 
 +
 + 
 + 
 +void keyMap(uint8 keySrc, uint8 keyDst, bool noset, uint8 keyCheck) { 
 +    if (key_status(keySrc)) { 
 +        if (!keyCheck) key_set(keySrc,​ FALSE); 
 +        else if (!key_status(keyCheck)) key_set(keySrc,​ FALSE); 
 +        keySet(keyDst,​ TRUE); 
 +    } 
 +    else if (!noset) key_set(keyDst,​ FALSE); 
 +
 + 
 +void keySet(uint8 key, bool flag) { 
 +    if (key_get(key) != flag) { 
 +        key_set(key,​ flag); 
 +    } 
 +
 +</​code>​
t2/gpc_scripting/usb_hid.1611140313.txt.gz · Last modified: 2021/01/20 05:58 by scachi