Global keyboard hooks

I was reading Daily Rituals: How Artists work. Inside the book, one of the writers recorded down how many words he wrote everyday to assess his performance.

I think it’s a good way to assess my work based on how many times I press the keyboard every hour or day. I did a bit digging and found out the Objective-C code for global keyboard hook from Mac OS internals.

After that, I found the Java library jnativehook. Maybe it make more sense to code it in Java so it’s across all platforms.

// alterkeys.c
// http://osxbook.com
//
// Complile using the following command line:
//     gcc -Wall -o alterkeys alterkeys.c -framework ApplicationServices
//
// You need superuser privileges to create the event tap, unless accessibility
// is enabled. To do so, select the "Enable access for assistive devices"
// checkbox in the Universal Access system preference pane.

#include <ApplicationServices/ApplicationServices.h>

// This callback will be invoked every time there is a keystroke.
//
CGEventRef
myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
                  CGEventRef event, void *refcon)
{
    // Paranoid sanity check.
    if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp))
        return event;
    
    // The incoming keycode.
    CGKeyCode keycode = (CGKeyCode)CGEventGetIntegerValueField(
                                                               event, kCGKeyboardEventKeycode);
    
    // Swap 'a' (keycode=0) and 'z' (keycode=6).
    if (keycode == (CGKeyCode)0)
        keycode = (CGKeyCode)6;
    else if (keycode == (CGKeyCode)6)
        keycode = (CGKeyCode)0;
    
    // Set the modified keycode field in the event.
    CGEventSetIntegerValueField(
                                event, kCGKeyboardEventKeycode, (int64_t)keycode);
    
    // We must return the event for it to be useful.
    return event;
}

int
main(void)
{
    CFMachPortRef      eventTap;
    CGEventMask        eventMask;
    CFRunLoopSourceRef runLoopSource;
    
    // Create an event tap. We are interested in key presses.
    eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp));
    eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0,
                                eventMask, myCGEventCallback, NULL);
    if (!eventTap) {
        fprintf(stderr, "failed to create event tap\n");
        exit(1);
    }
    
    // Create a run loop source.
    runLoopSource = CFMachPortCreateRunLoopSource(
                                                  kCFAllocatorDefault, eventTap, 0);
    
    // Add to the current run loop.
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource,
                       kCFRunLoopCommonModes);
    
    // Enable the event tap.
    CGEventTapEnable(eventTap, true);
    
    // Set it all running.
    CFRunLoopRun();
    
    // In a real program, one would have arranged for cleaning up.
    
    exit(0);
}

Day 3 – Creating a programming-friendly font

Started drawing the letters with rectangles instead of lines. It was quite challenging to make the joints look nice. The last stroke for “R” is a bit short now. I’ll improve it later.

The experimental codes has different drawing methods, like “drawBarGlyph”, “drawTriangleGlyph” etc. The new code uses “java.lang.reflect.*” package, which allows me to toggle between different methods by String. I wish I had used it in previous projects to avoid all the switch statements. Here’s an example on Stackoverflow. Using Reflection/Mirror is a very bad programming practice in general. But for Processing sketches, it’s perfect.

bar, bar variation and triangle drawing modes.

rectangle, rectangle variation and triangle drawing modes.

Day 1 – Creating a programming-friendly font

I’m trying to create a “font” which I can easily tweak the looks using programming.

use rectangle to form all letters

Font planning

Using rectangles as building blocks. The letters/glyphs will be easy to draw with codes. There are a few ideas I’m trying to explore:

  • Inaccurate drawing: I find normal fonts which reproduce the result for each letter is predictable and rigid. If the same letter is drawn a little bit differently every time. The result may look more organic.
  • Animation: ‘A’ can transform to ‘B’ is quite magical
  • Physics Engine: Letters constructed with physical features using Box2D will give text new meanings. Make ‘A’ fall apart and become bouncing rectangles. Make ‘A’ collide into ‘B’ or ‘PENIS’ messes up ‘VAGINA’
  • Generative letters: Vines and flowers form words.
  • 3D words

Here’s day 1’s progress.

Day 1 progress

Day 1 progress, normal drawing

Here’s an example of lousy/unfaithful drawing

screengrab2

Lousy/Unfaithful drawing

Will my codes be beautiful?

Here are the steps:

  1. Copy some codes from Xcode and paste into Pages
  2. Set the background color of the pages to black
  3. Save it as a PDF file
  4. Export the PDF file into images with Adobe Acrobat Pro
  5. Use Processing to draw the images onto a PGraphics
  6. Save the PGraphics into an image file.
  7. After 200 iterations…

git ignore global settings