Skip to content
eberle1080 edited this page Apr 12, 2011 · 3 revisions

Thanks for your interest in Kaptivate!

Kaptivate is under heavy development right now, which is to say that I do what I can when I feel like it. It is making some progress, however. If you're getting all antsy about how long this is going to take, grab a shovel and dig in! Contact me and you can help out. I'll give you something to do.

The ultimate goal of Kaptivate is simple. Tell the system which devices you want to handle (using a regular expression) and then simply handle the events until you unregister. When all is said and done, you'll be able to (1) tell which device generated the event, (2) get details about the event, and (3) control whether other apps will receive the event.

Hopefully when it's all done it will be this easy to use. Here's how I want it to work:

#include "stdafx.h"

#include <iostream>
#include <vector>
#include "kaptivate.hpp"
#include "kaptivate_exceptions.hpp"

using namespace std;
using namepsace Kaptivate;

// Print out information about a keyboard event. Called for all keyboards.
class KeyPrinter : public KeyboardHandler
{
public:
    virtual void HandleKeyEvent(KeyboardEvent& evt)
    {
        // Show some info about the event                                                                                                                   
        cout << "Got a keyboard event:" << endl;
        cout << " device: " << evt.getDeviceInfo()->name << endl;
        cout << " vkey: " << evt.getVkey() << endl;
        cout << " scan code: " << evt.getScanCode() << endl;
        cout << " key up?: " << evt.getKeyUp() << endl;
        cout << endl;
    }
};

// Consume spacebar events (from a specific keyboard)
class SpaceEater : public KeyboardHandler
{
public:
    virtual void HandleKeyEvent(KeyboardEvent& evt)
    {
        // Decide whether or not to consume the keystroke                                                                                                   
        if(evt.getVkey() == VK_SPACE)
        {
            evt.setDecision(CONSUME);
            cout << "Nom nom nom" << endl << endl;
        }
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    KaptivateAPI* kaptivate = NULL;
    SpaceEater spaceHandler;
    KeyPrinter printHandler;

    try
    {
        // Initialize the Kaptivate singleton
        kaptivate = KaptivateAPI::getInstance();

        // Print out a list of attached keyboards
        vector<KeyboardInfo> keyboards = kaptivate->enumerateKeyboards();
        vector<KeyboardInfo>::iterator kit;
        for(kit = keyboards.begin(); kit != keyboards.end(); kit++)
            cout << "Keyboard found: " << (*kit).name << endl;
        cout << endl;

        // Register for events. NOTE: The first registered for a device is the last executed.
        // So make sure to register in the order that makes sense.

        // Eat spaces from a specific keyboard device
        kaptivate->registerKeyboardHandler(".*884b96c3-56ef-11d1-bc8c-00a0c91405dd.*", spaceHandler);

        // Print out event information for all keyboards (since this is registered last, we will
        // get the events first and be able to print them before they are consumed).
        kaptivate->registerKeyboardHandler(".*", printHandler);

        // Start the capture hooks, but only for the keyboard
        kaptivate->startCapture(false, true);

        // Main program event loop goes here
        ...

        // Stop the capture hooks when exiting and clean up
        kaptivate->stopCapture();

        // Clean up Kaptivate
        kaptivate = NULL;
        KaptivateAPI::destroyInstance();
    }
    catch(KaptivateException &kex)
    {
        cout << "Kaptivate exception: " << kex.what() << endl;
    }

    return 0;
}
Clone this wiki locally