AppleScript detect key press by user alternatives

I have searched and found that AppleScript is unable to detect key presses by the user. I would like to know is there any other alternatives to do this?

I am building a script for a specific tabs in all Internet browsers, IE, FF, Chrome, Safari.

  1. The specific tab will be blur out, title tab, favicon, URL in address bar will be masked or replace.

  2. When the user press "tab" key for example, it will temporary remove all the things that is done in step 1 and show the original content.

  3. If the user press tab again, it will go back to the state in step 1.

I have only managed to temporary make the body blur out but not the rest of step 1 and the detecting of key press.

My code:

tell application "Google Chrome"

    set allTabs to front window's tab
    repeat with eachTabs from 1 to count of allTabs
        set eachTab to item eachTabs of allTabs
        if URL of eachTab = "http://c10h20o.com/uniqlo/2016/0923/" then
            set currentTab to eachTabs
        end if
    end repeat


    tell front window

        tell tab currentTab

            execute javascript "document.body.style.filter = 'blur(0)'" --this line is working

            execute javascript " --this javascript always returns me missing value

            $(document).keypress(function(event){

                    var keycode = (event.keyCode ? event.keyCode : event.which);
                    if(keycode == '13'){
                        alert('You pressed a enter key in somewhere');    
                    }

            });         
            "
        end tell



    end tell
end tell

I am completely new to this and it's my first time doing this.

I have asked on stack overflow but no one has replied. Hope you guys can help me out with this.


Solution 1:

One way would be to use Hammerspoon to detect key presses as in this example. You could use something like this:

hs.hotkey.bind({"cmd", "alt", "ctrl"}, "I", function()
  ok,result = hs.applescript('tell Application "iTunes" to artist of the current track as string')
    hs.notify.new({title="Hammerspoon", informativeText=result}):send()
end)

Pressing ⌘+⌥+ctrl+I will give you a notification with the name of the artist, in this case.