Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import
- std.utf,
- std.conv,
- ws.io,
- ws.decode,
- ws.time,
- ws.exception,
- ws.gui.input,
- ws.gui.osWindow;
- version(Windows)
- import core.sys.windows.windows;
- // settings
- long mouseBox = 50;
- double tick = 0.01;
- double inputDelay = 0.009;
- bool borderReset = true;
- ushort button = Keyboard.space;
- ushort buttonEmulate = Mouse.buttonMiddle;
- // internal
- osWindow window;
- long[2] screenSize;
- long[2] lastMouse;
- long[2] internalCursor;
- bool dragging;
- void main(){
- Decode.file("settings.cfg", (name, value, b){
- switch(name){
- case "button":
- case "emulate":
- if(value !in Keys)
- exception("\"%\" is not a valid button", value);
- if(name == "button")
- button = cast(ushort)Keys[value];
- else
- buttonEmulate = cast(ushort)Keys[value];
- break;
- case "border":
- mouseBox = to!long(value);
- break;
- case "borderReset":
- borderReset = (value == "false" ? false : true);
- break;
- case "tick":
- tick = to!double(value);
- break;
- case "inputDelay":
- inputDelay = to!double(value);
- break;
- default:
- exception("\"%\" is not a setting", name);
- }
- });
- while(true){
- try {
- if(window && wm.findWindow("Dota 2")){
- if(wm.isKeyDown(button) != dragging){
- dragging = !dragging;
- sendButton(buttonEmulate, dragging);
- internalCursor = wm.getCursorPos();
- lastMouse = internalCursor;
- window.setCursor(dragging ? Mouse.cursor.none : Mouse.cursor.inherit);
- }
- time.sleep(tick);
- if(dragging){
- auto pos = wm.getCursorPos();
- onMouseMove(pos.x, pos.y);
- }
- }else{
- window = wm.findWindow("Dota 2");
- screenSize = window.getScreenSize();
- }
- }
- catch(WindowNotFound e){
- writeln("Could not find Dota 2 window");
- time.sleep(1);
- }
- catch(Exception e)
- writeln(e);
- }
- }
- void resetCursor(){
- sendButton(buttonEmulate, false);
- time.sleep(inputDelay);
- window.setCursorPos(cast(int)internalCursor.x, cast(int)internalCursor.y);
- time.sleep(inputDelay);
- sendButton(buttonEmulate, true);
- lastMouse = internalCursor;
- }
- void onMouseMove(long x, long y){
- long[2] diff = [x - lastMouse.x, y - lastMouse.y];
- lastMouse = [x, y];
- internalCursor = [
- clamp(internalCursor.x-diff.x, screenSize.x/2-mouseBox, screenSize.x/2+mouseBox),
- clamp(internalCursor.y-diff.y, screenSize.y/2-mouseBox+50, screenSize.y/2+mouseBox)
- ];
- resetCursor();
- }
- void sendButton(ushort b, bool pressed){
- window.sendMessage(pressed ? WM_KEYDOWN : WM_KEYUP, cast(WPARAM)b, 0);
- INPUT ip;
- ip.type = INPUT_KEYBOARD;
- ip.ki.wScan = 0;
- ip.ki.time = 0;
- ip.ki.dwExtraInfo = 0;
- ip.ki.wVk = b;
- ip.ki.dwFlags = (pressed ? 0 : KEYEVENTF_KEYUP);
- SendInput(1, &ip, INPUT.sizeof);
- }
- T clamp(T, C: T)(T a, C min, C max){
- return (a < min ? min : (a > max ? max : a));
- }
- ref T x(T)(ref T[2] a){
- return a[0];
- }
- ref T y(T)(ref T[2] a){
- return a[1];
- }
- extern(Windows):
- LPARAM MAKELPARAM(WPARAM a, WPARAM b){
- return (a & 0xffff) | ((b & 0xffff) << 16);
- }
- const int INPUT_KEYBOARD = 1;
- const int KEYEVENTF_KEYUP = 2;
- uint SendInput(uint cInputs, INPUT* pInputs, int cbSize);
- struct INPUT {
- DWORD type;
- union {
- MOUSEINPUT mi;
- KEYBDINPUT ki;
- HARDWAREINPUT hi;
- };
- }
- struct MOUSEINPUT {
- LONG dx;
- LONG dy;
- DWORD mouseData;
- DWORD dwFlags;
- DWORD time;
- ULONG_PTR dwExtraInfo;
- }
- struct KEYBDINPUT {
- WORD wVk;
- WORD wScan;
- DWORD dwFlags;
- DWORD time;
- ULONG_PTR dwExtraInfo;
- }
- struct HARDWAREINPUT {
- DWORD uMsg;
- WORD wParamL;
- WORD wParamH;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement