Skip to content

Instantly share code, notes, and snippets.

@DanBrink91
Created October 21, 2014 19:04
Show Gist options
  • Select an option

  • Save DanBrink91/7c130e71506942335332 to your computer and use it in GitHub Desktop.

Select an option

Save DanBrink91/7c130e71506942335332 to your computer and use it in GitHub Desktop.

Revisions

  1. DanBrink91 created this gist Oct 21, 2014.
    22 changes: 22 additions & 0 deletions text.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    function getSelectionText() {
    var text = "";
    if (window.getSelection) {
    text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
    text = document.selection.createRange().text;
    }
    return text;
    }
    var keysDown = {};

    window.addEventListener("keyup", function(e){
    keysDown[e.keyCode] = false;
    });
    window.addEventListener("keydown", function(e){
    keysDown[e.keyCode] = true;
    // ctrl + `
    if(keysDown[192] && keysDown[17]){
    var text = getSelectionText();
    // do something with selected text
    }
    });