AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Detect if input boxes are focused on web pages

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
keyboardfreak



Joined: 09 Oct 2004
Posts: 143
Location: Budapest, Hungary

PostPosted: Thu Aug 28, 2008 6:47 pm    Post subject: Detect if input boxes are focused on web pages Reply with quote

Using the same technique as described in this topic i did a little proof of concept implementation of input box focus detection.

It is useful if you want to add custom single-key hotkeys to webpages you visit frequently, but don't want them to interfere with normal typing (e.g. custom hotkeys for GMail, etc.). If an input text box is focused then the script can send a normal character, otherwise it performs the assigned function.

The distinction can be made by checking the window title which reflects the current focus status.

The Javascript snippet below was tested on Opera only. It probably works on Firefox too. Let me know if it does.

Update: Added document focus checking, so that the AHK script sending the characters can detect if focus is on the address bar.

I did a quick try on Firefox and the script didn't work because of Javascript differences (different objects in the DOM). I don't know enough about JS to debug the problem, so it's up to someone more knowledgeable in JS to fix it for Firefox and IE.

Code:
function ahk_focus_indicate(e)
{
  document.title = original_title + " [input]";
  inputfocus = true;
}

function ahk_nofocus_indicate(e)
{
  document.title = original_title + " [noinput]";
  inputfocus = false;
}

function ahk_docfocus_indicate(e)
{
  if (!inputfocus)
    document.title = original_title + " [doc]";
}

function ahk_docnofocus_indicate(e)
{
  document.title = original_title + " [nodoc]";
}

var original_title;
var inputfocus = false;

document.addEventListener(
  'load',
  function (e) {
    original_title = document.title;

    document.onblur = ahk_docnofocus_indicate;
    document.onfocus = ahk_docfocus_indicate;

    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++)
    {
      var input = inputs[i];

      if (input.type != "text")
   continue;

      input.onfocus = ahk_focus_indicate;
      input.onblur = ahk_nofocus_indicate;
    }

 }, false);


Last edited by keyboardfreak on Mon Sep 01, 2008 9:38 am; edited 1 time in total
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 1033

PostPosted: Thu Aug 28, 2008 9:13 pm    Post subject: Reply with quote

Code:
JS=
(function ahk_focus_indicate(e)
{
  document.title = original_title + " [focus]";
}

function ahk_nofocus_indicate(e)
{
  document.title = original_title + " [nofocus]";
}

var original_title;

document.addEventListener(
  'load',
  function (e) {
    original_title = document.title;

    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++)
    {
      var input = inputs[i];

      if (input.type != "text")
   continue;

      input.onfocus = ahk_focus_indicate;
      input.onblur = ahk_nofocus_indicate;
    }
 }, false);
)
COM_Init()
COM_Error(0)
pageSearched:="Detect if input boxes are focused on web pages"
Loop, %   COM_Invoke(psw := COM_Invoke(psh:=COM_CreateObject("Shell.Application"), "Windows"), "Count")
   {
      LocationName:=COM_Invoke(pwb:=COM_Invoke(psw, "Item", A_Index-1), "LocationName")
      IfInString,LocationName,%pageSearched%
         Break
      COM_Release(pwb)
   }
COM_Invoke(pwin:=COM_Invoke(document:=COM_Invoke(pwb,"Document"),"Parent Window"),"execScript",JS)
COM_Release(pwin),COM_Release(document),COM_Release(pwb)
COM_Term()
The above would be a quick adaption FOR IE of detecting feild focus on this page "Detect if input boxes are focused on web pages"
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
keyboardfreak



Joined: 09 Oct 2004
Posts: 143
Location: Budapest, Hungary

PostPosted: Fri Aug 29, 2008 6:47 am    Post subject: Reply with quote

tank wrote:
The above would be a quick adaption FOR IE of detecting feild focus on this page "Detect if input boxes are focused on web pages"


Cool! If the same JS works on IE then it most probably works on FireFox too, so we have input focus detection for all the major browsers.
Back to top
View user's profile Send private message
Guest






PostPosted: Fri Aug 29, 2008 11:25 am    Post subject: Re: Detect if input boxes are focused on web pages Reply with quote

keyboardfreak wrote:
Using the same technique as described in this topic i did a little proof of concept implementation of input box focus detection.


@keyboardfreak, may I suggest that you post a link in the referenced post to this thread. It occurs to me that those reading the referenced post would find this post useful. Smile
Back to top
tank



Joined: 21 Dec 2007
Posts: 1033

PostPosted: Fri Aug 29, 2008 12:22 pm    Post subject: Reply with quote

i want to be clear i didnt test what i posted so i dont know if the javascript works correctly
but all the rest of it providing the javaqscript works would definately work in IE
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
keyboardfreak



Joined: 09 Oct 2004
Posts: 143
Location: Budapest, Hungary

PostPosted: Fri Aug 29, 2008 4:08 pm    Post subject: Re: Detect if input boxes are focused on web pages Reply with quote

Anonymous wrote:
@keyboardfreak, may I suggest that you post a link in the referenced post to this thread. It occurs to me that those reading the referenced post would find this post useful. Smile


Good idea. Done.
Back to top
View user's profile Send private message
keyboardfreak



Joined: 09 Oct 2004
Posts: 143
Location: Budapest, Hungary

PostPosted: Fri Aug 29, 2008 4:09 pm    Post subject: Reply with quote

tank wrote:
i want to be clear i didnt test what i posted so i dont know if the javascript works correctly
but all the rest of it providing the javaqscript works would definately work in IE


Okay, then it still needs to be tested on FF and IE. Any volunteers? I use Opera, so I did my part.
Back to top
View user's profile Send private message
keyboardfreak



Joined: 09 Oct 2004
Posts: 143
Location: Budapest, Hungary

PostPosted: Mon Sep 01, 2008 9:40 am    Post subject: Reply with quote

Added some improvents and more info on Firefox in the first post.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group