How to type in Dvorak but keep Qwerty shortcuts

Every Dvorak user knows the pain: shortcuts in every program on earth is designed with Qwerty in mind. Cut – Copy – Paste is supposed to be next to each other but it’s not so in Dvorak.

As A Dvorak user, I’ve had to find and try many solutions for this problem over the years. It involved all sort of hacks and modifications to the deepest parts of the operating system: creating custom keyboard layouts, modifying the registry, write a new input method etc. Even so, it didn’t work in some applications (looking at you, IntelliJ, Java and Firefox – the bugs isn’t even fixed yet, and they are almost 10 years old!).

Almost 10 years later, in 2019, I finally found solutions that work for all major operating systems, and here they are:

MacOS

Apple makes great software, even though I don’t like their business practices, that’s an undeniable fact. MacOS is the simplest of the three OSes: Simply add Dvorak-QWERTY as an input method and you are good to go!

Windows

Used to be the second-easiest OS to work with, but recently Microsoft started cracking down on custom software and drivers so any custom keyboard layout not provided by them will get wiped out (and basically becomes very buggy with Windows 8 and up).

My solution? Use AutoHotKey! Yes, it’s an extra install, but it works reliably across all machines I’ve used with little setup required.

How to use Dvorak-QWERTY with AutoHotKey on Windows

  1. Install and set a Dvorak layout as your default (which language doesn’t matter)
  2. Install AutoHotKey
  3. Save this gist to your machine
  4. Optional: Convert the .ahk file to .exe with AutoHotKey so you can use it elsewhere without AHK
  5. Put the .ahk file in your startup folder
  6. Profit!

Notes about the AHK script

  • It will switch the layout to Qwerty when control keys or a combination of them is pressed (Ctrl, Alt, Win, Ctrl + Alt, Ctrl + Win)
  • It will disable itself when the input language is Korean (code = 68289554) so you can type Korean characters uninterfered. You can find similar code for Japanese, Chinese, etc. using AHK’s inspection tool
  • It will disable itself when scroll lock is on. This is for exceptional cases when you want to use Qwerty without pressing modifiers key
  • If you need additional combination of control keys, you must copy a whole section and add the control keys manually. For some reason, AHK’s * doesn’t work properly with control keys no there’s no way to make the script shorter

Linux

What worked for me: modifying the xkb keymaps! This idea came when I tried to replicate the AutoHotKey solution on Linux. I found source code of past projects that tried to achieve this. Sadly no step by step guide on how to apply them. Here’s how:

  • Press the plus button
  • Choose English (United States)
  • Choose any of the English (Dvorak-Qwerty), the flavor you prefer
  • Now switch the keyboard layout to your newly added layout
  • You’re done!

Conclusion

Dvorak is a good ergonomic layout but it hasn’t seen more widespread adoption due to entry barriers like the QWERTY shortcut problem. I hope this post solved part of that problem.

To read: The twelve factor app

Introduction

In the modern era, software is commonly delivered as a service: called web apps, or software-as-a-service. The twelve-factor app is a methodology for building software-as-a-service apps that:

  • Use declarative formats for setup automation, to minimize time and cost for new developers joining the project;
  • Have a clean contract with the underlying operating system, offering maximum portability between execution environments;
  • Are suitable for deployment on modern cloud platforms, obviating the need for servers and systems administration;
  • Minimize divergence between development and production, enabling continuous deployment for maximum agility;
  • And can scale up without significant changes to tooling, architecture, or development practices.

The twelve-factor methodology can be applied to apps written in any programming language, and which use any combination of backing services (database, queue, memory cache, etc).

The twelve factors

I. Codebase

One codebase tracked in revision control, many deploys

II. Dependencies

Explicitly declare and isolate dependencies

III. Config

Store config in the environment

IV. Backing services

Treat backing services as attached resources

V. Build, release, run

Strictly separate build and run stages

VI. Processes

Execute the app as one or more stateless processes

VII. Port binding

Export services via port binding

VIII. Concurrency

Scale out via the process model

IX. Disposability

Maximize robustness with fast startup and graceful shutdown

X. Dev/prod parity

Keep development, staging, and production as similar as possible

XI. Logs

Treat logs as event streams

XII. Admin processes

Run admin/management tasks as one-off processes

How to write a C MQTT client using Mosquitto

Introduction

How to write a C MQTT client using Mosquitto The 2018 version, based upon this excellent post by Kevin Boone:

Writing an MQTT client C for ActiveMQ from the ground up

The article above is a good and easy starting point, but it hasn’t been updated for 2 years so when you run it with the latest version of Mosquitto, it doesn’t work – and it’s a bit hacky (using “sleep” to avoid a concurrency problem).

So I analyzed the latest mosquitto_pub code from mosquitto repository itself to see how it’s working, and this article is the result.

What’s changed

  • There’s a queue inside mosquitto, `mosquitto_loop` must be called for it to be processed. Alternatively, you can also use the `mosquitto_loop_start` and `mosquitto_loop_stop`
  • I added asynchronous (callback) processing to wait for calls to complete, instead of the ole’ sleep function
  • It’s 2018! Everyone is adopting HTTPS. Accordingly, your MQTT traffic shouldn’t be left bare for all to see! Let’s use TLS to encrypt the traffic

The code

How to use callback

I want to publish just once message, so my flow is the following

  • On connect complete -> publish a message
  • On publish complete -> start to disconnect
  • On disconnect complete -> exit the loop and return control to the main thread. If you don’t wait for this, data may not even get sent!

To do this, I set up 3 “hooks” (callback function), like this

mosquitto_connect_callback_set(mosq, my_connect_callback);

mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);

mosquitto_publish_callback_set(mosq, my_publish_callback);
And then write the callback functions to execute my flow
void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
{
    int rc = MOSQ_ERR_SUCCESS;
    if(!result){
        printf("Sending message...\n");
        rc = mosquitto_publish(mosq, &mid_sent, MQTT_TOPIC, strlen(text), text, qos, retain);
        if(rc){
            switch(rc){
                case MOSQ_ERR_INVAL:
                    fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n");
                    break;
                case MOSQ_ERR_NOMEM:
                    fprintf(stderr, "Error: Out of memory when trying to publish message.\n");
                    break;
                case MOSQ_ERR_NO_CONN:
                    fprintf(stderr, "Error: Client not connected when trying to publish.\n");
                    break;
                case MOSQ_ERR_PROTOCOL:
                    fprintf(stderr, "Error: Protocol error when communicating with broker.\n");
                    break;
                case MOSQ_ERR_PAYLOAD_SIZE:
                    fprintf(stderr, "Error: Message payload is too large.\n");
                    break;
            }
            mosquitto_disconnect(mosq);
        }
    } else {
        if(result){
            fprintf(stderr, "%s\n", mosquitto_connack_string(result));
        }
    }
}

void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc)
{
    printf("Disconnected!\n");
    connected = false;
}

void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
{
    printf("Published!\n");
    if(disconnect_sent == false){
        mosquitto_disconnect(mosq);
        disconnect_sent = true;
    }
}

How to process the queue with mosquitto_loop

In the main trunk of your code, do this

int rc;
do {
//network동작 끝나기 전에 모스키토 동작을 막기위해 잠깐 딜레이가 필요
  rc = mosquitto_loop(mosq, -1, 1);
} while (rc == MOSQ_ERR_SUCCESS && connected);

How to add TLS to the connection process

Before connecting, set TLS options with mosquitto_tls_set

mosquitto_username_pw_set(mosq, MQTT_USERNAME, MQTT_PASSWORD);
mosquitto_tls_set(mosq, "ca-cert.pem", NULL, NULL, NULL, NULL);
int ret = mosquitto_connect(mosq, MQTT_HOSTNAME, MQTT_PORT, 0);

Complete publish – subscribe sample

Available at https://github.com/thanhphu/mosquitto-sample. Happy cloning!

 

Find and block unsafe content on your wordpress installation

For some reason, my WP installation decided to load some files via http instead of https

To find out which file it is, I used the Javascript console

It’s wp-emoji in this case, and I don’t use emojis on my site, so I decided to block it by adding this to my theme’s functions.php

 

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

 

How to turn on the quit confirmation prompt in Chrome

Recently Chrome 69 started to disrespect the keyboard mapping on my Mac, which result in the Cut key becomes the Quit key! I searched for the option to confirm before quitting but the results are all outdated… Unbelievable! So here’s how:

On Linux and Windows

Go to chrome://flags

Search for Warn before quitting

On Mac

Chrome Menu / Warn Before Quitting