padma

padma

  • NA
  • 11
  • 11.4k

Detect when user switchs tab of browser(IE ,Chrome,firefox)

Apr 9 2014 3:41 AM
 

How to detect when user switchs tab of a browser?

How to detect when user switchs to other sheet of an excel?

I will explain why am i interested to know?

i am working on  how much time user is spent on each active application.

Right now i am using SetWinEventHook() and winproc() call back funtion to detect active application.But problem with this is it does not detects when user switches tab of any browser and also switches from one excel sheet to other excel sheet.

So to slove this i came to know ,in two ways user can switch between tabs or sheets using two actions.
1)using keyboard short cuts(ctrl+tab,ctrl+1...for browsers and ctrl+pagedwn,ctrl+pgup for sheets of excel)
2)using  Mouse click  (left click on tab or sheets of excel)
so i used Useractivityhook and accessed its related global keydown and mouse click events

 The code is as follows

    private void globalHook_KeyDown(object sender, KeyEventArgs e)
    {
    try
    {
        currKey = e.KeyValue;
        switch (appName.Trim())
        {
            case "Microsoft Excel":
                    switch (currKey.ToString().Trim() + prevKey.ToString().Trim())
                    {
                        //33=PageUp, 34=PageDwn,  162=left Ctrl ,  163=Right ctrl
                        //PageUp + Left Ctrl .
                        case "33162":
                        //PageDwn + Left Ctrl
                        case "34162":
                        //PageUp + Right Ctrl
                        case "33163":
                        //PageDwn + Right Ctrl
                        case "34163":
                            isPgeNavigated = true;
                            //comparing previous sheet name with the current sheet.
                            //if changed i came to know user switch to new sheet
                            break;
                        //Left StartWindow=91,76=L
                        case "7691":
                        //Right StartWindow=92,76=L
                        case "7692":
                            break;
                        default:
                            isPgeNavigated = false;
                            break;
                    }
                    prevKey = currKey;
                break;
            case "Internet Explorer":
            case "Firefox":
            case "Google Chrome":
                    switch (currKey.ToString().Trim() + prevKey.ToString().Trim())
                    {
                        //Tab =9,lctrl=162,lshift= 160,RCtrl= 163,RShift= 161,w=87,n=78,D1 =49,D9=57,Num 1 =97,Num 9=105
    
                        //Moves Next tab
                        case "9162":
                        case "9163":
                        //Moves FirstTab
                        case "49162":
                        case "49163":
                        //Moves First Tab Using Numlock Keypad
                        case "97162":
                        case "97163":
                        //Moves Last tab
                        case "57162":
                        case "57163":
                        //Moves Last Tab using NumLock Keypad
                        case "105162":
                        case "105163":
                        //Close
                        case "87162":
                        case "87163":
                         //comparing previous url with the current url.
                          //if changed i came to know user switch to new tab
                            break;
                        // CTRL + SHIFT
                        case "160162":
                        case "160163":
                        case "161162":
                        case "161163":
                            ShiftPrevTab = true;
                            break;
                        default:
                            switch (currKey)
                            {
                                case 9:
                                    // CTRL + SHIFT + TAB 
                                    if (ShiftPrevTab)
                                    {
                                   //comparing previous url with the current url.
                                    //if changed i came to know user switch to new tab
                                    }
                                    break;
                                default:
                                    break;
                            }
                            break;
                    }
                    prevKey = currKey;
                }
                break;
            default:
               break;
       }
            
        
    }
    catch (Exception ex)
    {
    }
    }
    
    private void globalHook_OnMouseActivity(object sender, MouseEventArgs e)
    {
     try
        {
          switch (e.Button)
          {
            case MouseButtons.Left:
                if (!string.IsNullOrEmpty(appName))
                {
                    switch (appName.Trim())
                    {
                        case "Microsoft Excel":
                        case "Internet Explorer":
                        case "Firefox":
                        case "Google Chrome":
                            //comparing previous url with the current url.
                           //if changed i came to know user switch to new tab
                            break;
                        default:
                            break;
                    }
                }
                break;
            default :
            break;
           }
       } 
    catch (Exception ex)
        {
        }
    }

As this is lengthy process and not a reliable solution ,i don't want to go this approach.

Can some one please help me is there any event that detects when user switches between tabs of browser or between sheets in an excel.Please share your ideas.