Capturing backspace in D3 / Javascript

I wanted to bind backspace on a web page, for a special UI. Had a hell of a time; the default behavior of backspace is to navigate the browser back a page in the history and it’s not easy to override. but long story short:

// Interaction: handle keyboard events (keydown to capture backspace)"body").on("keydown", key);

function key() {
    if (code == 8) {
        // Prevent the browser from going back in the URL history

        // Do your thing here.

Some things that don’t work: returning false in the event listener (it’s ignored), calling stopPropagation(), or binding keypress instead of keydown. Browser keyboard events are a total mess.