I wrote a program to get you started. It needs simpledisplay.d and color.d from my github https://github.com/adamdruppe/arsd

Just download those two files and put them in your folder along with the following contents as hotkey.d and you should get started.

I tested on Windows 7 to hotkey type some stuff into a Notepad window.

Here's the code:



// compile: dmd hotkey.d simpledisplay.d color.d -L/SUBSYSTEM:WINDOWS:5.0

// helper function to send a string. Call with like "hello!"w -- notice
// the w at the end of the string literal.
void sendString(wstring s) {
        INPUT[] inputs;
        inputs.reserve(s.length * 2);

        foreach(wchar c; s) {
                // the basic pattern here is to send a unicode key
                // pressed then released
                INPUT input;
                input.type = INPUT_KEYBOARD;
                input.ki.wScan = c;
                input.ki.dwFlags = KEYEVENTF_UNICODE;
                inputs ~= input;

                input.ki.dwFlags |= KEYEVENTF_KEYUP; // released...
                inputs ~= input;
        }

        // then send it to the operating system
if(SendInput(inputs.length, inputs.ptr, INPUT.sizeof) != inputs.length) {
                import std.stdio;
                writeln("SendInput failed");
        }
}

// the SendInput function can also send other keys, see the MSDN link
// I gave in my last email for details.

void main() {
        // uses my simpledisplay.d to pop up a quick window
        import simpledisplay;

        enum hotkey_id = 1; // arbitrary unique ID for the program

        auto window = new SimpleWindow(100, 50);
window.handleNativeEvent = delegate int(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
                if(hwnd !is window.impl.hwnd)
                        return 1; // we don't care...
                switch(msg) {
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646279%28v=vs.85%29.aspx
                        case WM_HOTKEY:
                                if(wParam == hotkey_id) {
// *** This is what happens when it is pressed!!! *** // MessageBoxA(window.impl.hwnd, "Hotkey", "Pressed!", MB_OK);
                                        sendString("Hey, it worked!"w);
                                        return 0;
                                }
                        goto default;
                        default: return 1; // not handled, pass it on
                }
                return 0;
        };

        string message = "Hotkey ready";

        // you can also pass modifiers or a capital ASCII char here
        // warning though: when it sends input, it still considers the
// modifiers down. So like if you make it MOD_ALT and 'K', and send // the string 'Hello'... alt is still down, so the program will think
        // the user hit alt+H - and thus bring up the Help menu!
        //
// *** This registers the key with the operating system ***
        if(!RegisterHotKey(window.impl.hwnd, hotkey_id, 0, VK_F2)) {
                message = "RegisterHotKey failed";
        }

        {
                auto painter = window.draw();
                painter.drawText(Point(0, 0), message);
        }
        window.eventLoop(0); // draw our window
}

// these are bindings to the necessary Windows API functions

import core.sys.windows.windows;

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx
extern(Windows) BOOL RegisterHotKey(HWND, int, UINT, UINT);
// http://msdn.microsoft.com/en-us/library/ms646310%28v=vs.85%29.aspx
extern(Windows) UINT SendInput(UINT, INPUT*, int);

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;
}

enum INPUT_MOUSE = 0;
enum INPUT_KEYBOARD = 1;
enum INPUT_HARDWARE = 2;

enum MOD_ALT = 0x1;
enum MOD_CONTROL = 0x2;
enum MOD_NOREPEAT = 0x4000; // unsupported
enum MOD_SHIFT = 0x4;
enum MOD_WIN = 0x8; // reserved

enum WM_HOTKEY = 0x0312;

enum KEYEVENTF_EXTENDEDKEY = 0x1;
enum KEYEVENTF_KEYUP = 0x2;
enum KEYEVENTF_SCANCODE = 0x8;
enum KEYEVENTF_UNICODE = 0x4;

Reply via email to