INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Catching MM Keys (QUESTION)  (Read 2114 times)

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Catching MM Keys (QUESTION)
« on: November 20, 2002, 01:03:16 pm »

Does anybody know how to catch MM keys?

In MJ 8, we create a process-level hook.  That works as long as we have the focus, or the keyboard driver forwards the message around.

The other choice is a 2 KB dll that acts as a global hook.  This does the job, but makes us nervous.

Is there a better way than a global hook to ensure we always get the messages?

Thanks for any help.
Logged
Matt Ashland, JRiver Media Center

RhinoBanga

  • Citizen of the Universe
  • *****
  • Posts: 1703
  • Developer
Re: Catching MM Keys (QUESTION)
« Reply #1 on: November 20, 2002, 08:52:39 pm »

Global hooks is how I do it in AV:



     //
     //  HOOK CALLBACKS
     //

     extern "C"
     {

           LRESULT CALLBACK MMCallback( int nCode, WPARAM wParam, LPARAM lParam )
           {
                 HWND            hWnd;

                 switch( nCode )
                 {
                 case HSHELL_APPCOMMAND:
   
                       //  Process the key
                       switch( GET_APPCOMMAND_LPARAM(lParam) )
                       {
                       case APPCOMMAND_MEDIA_NEXTTRACK:
                       case APPCOMMAND_MEDIA_PREVIOUSTRACK:
                       case APPCOMMAND_MEDIA_STOP:
                       case APPCOMMAND_MEDIA_PLAY_PAUSE:

                             //  Find the MJ window
                             hWnd = FindWindowEx(0, 0, "MJFrame", "MEDIA JUKEBOX");
                             if ( hWnd )
                             {
                                   PostMessage( hWnd, WM_APPCOMMAND, wParam, lParam );
                             }

                             break;
                       }
   
                 }

                 return( CallNextHookEx( m_hShellHook, nCode, wParam, lParam ) );
           }

     }
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Re: Catching MM Keys (QUESTION)
« Reply #2 on: November 21, 2002, 04:34:41 am »

How do you set the hook?

As far as I can tell, if you set it in a program, it's process specific.  You need to put it in a DLL for it to be global.

Is that right, or no?

Thanks Rhino!
Logged
Matt Ashland, JRiver Media Center

RhinoBanga

  • Citizen of the Universe
  • *****
  • Posts: 1703
  • Developer
Re: Catching MM Keys (QUESTION)
« Reply #3 on: November 21, 2002, 05:01:06 am »

That's right as the DLL gets mapped into the process space of the app ... but that's ok from my point of view as AV is a DLL.

Here's how I set the hook:


           if( m_bEnableMM )
           {
                 m_hShellHook = SetWindowsHookEx( WH_SHELL, MMCallback, AfxGetInstanceHandle(), 0);
           }




And in my dll's .DEF file I have:


; AlbumView2.def : Declares the module parameters.

LIBRARY      "AlbumView2.DLL"

EXPORTS
     DllCanUnloadNow     @1 PRIVATE
     DllGetClassObject   @2 PRIVATE
     DllRegisterServer   @3 PRIVATE
     DllUnregisterServer      @4 PRIVATE
     MMCallback
     KeybCallback

SECTIONS
  .SHARDATA   Read Write Shared




Notice the .SHARDATA section ... that's used if you want to share data between processes.   Whenever a track changes in MJ I update the following two hotkey strings:


#pragma data_seg(".SHARDATA")
char                        m_szHK1[ 8192 ]            =      { "" };
char                        m_szHK2[ 8192 ]            =      { "" };
#pragma data_seg()



According to the tokens the user has specified.


Then whenever the user issues a hotkey I can paste the contents to the clipboard, e.g.:


           LRESULT CALLBACK KeybCallback( int nCode, WPARAM wParam, LPARAM lParam )
           {

                 static bool            bProcessed                  =      false;

                 switch( nCode )
                 {
                 case HC_ACTION:
   
                       //  Process the key
                       if( (wParam == VK_F11  ||  wParam == VK_F12)  &&  (lParam & ALT)  &&  (GetAsyncKeyState( VK_CONTROL ) & 0x80000000) )
                       {
                             if( !bProcessed )
                             {
                                   HANDLE            hMem;
                                   LPCTSTR            pszData;
                                   PVOID            pvMemory;
                                   DWORD            dwLength;

                                   //  Mark as being processed
                                   bProcessed = true;

                                   //  Point to the data in question
                                   pszData = (( wParam == VK_F11 ) ? m_szHK1:m_szHK2);

                                   //  Allocate a memory handle
                                   if( hMem = GlobalAlloc( GMEM_MOVEABLE | GMEM_SHARE, dwLength = (strlen( pszData ) + 1) ) )
                                   {
                                         //  Open it
                                         if( pvMemory = GlobalLock( hMem ) )
                                         {
                                               //  Copy in the string
                                               CopyMemory( pvMemory, pszData, dwLength );

                                               //  Unlock the handle
                                               GlobalUnlock( hMem );

                                               //  Send to the clipboard
                                               if( OpenClipboard( NULL ) )
                                               {
                                                     EmptyClipboard();
                                                     SetClipboardData( CF_TEXT, hMem );
                                                     CloseClipboard();
                                               }
                                         }
                                   }

                                   //  Unlock the memory
                                   GlobalUnlock( hMem );

                             }
                             else
                             {
                                   bProcessed = false;
                             }

                             break;
                       }

                       break;
                 }

                 return( CallNextHookEx( m_hKeybHook, nCode, wParam, lParam ) );
           }




Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Re: Catching MM Keys (QUESTION)
« Reply #4 on: November 21, 2002, 05:08:55 am »

Have you had reports of AV crashing other programs?

It seems like a pretty big DLL to be mapping into every process.

How dangerous / resource intensive is it?

Thanks again Rhino.
Logged
Matt Ashland, JRiver Media Center

RhinoBanga

  • Citizen of the Universe
  • *****
  • Posts: 1703
  • Developer
Re: Catching MM Keys (QUESTION)
« Reply #5 on: November 21, 2002, 05:36:03 am »

I have heard nothing dude.

I think it's pretty safe and I use it *all* the time at home since I can't be bothered loading the bloatware software that comes with my Logitec keyboard.   It has no impact on my CPU as far as I can see.

I agree that the DLL is a reasonable size but I personally think 800k is nothing these days and I presume that that it's only the data segment that actually gets mapped as WIN32 code shares ... but I'm not 100% sure on that.

As you mentioned before I recon you should provide a small DLL that just does the keyboard hook and fires off messages with MJ.   Then within MJ just provide the ability to either a) handle the APPCOMMAND messages or b) allow the specification of a key combination and what action to perform ... for example play.
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42373
  • Shoes gone again!
Re: Catching MM Keys (QUESTION)
« Reply #6 on: November 26, 2002, 05:28:11 am »

Just wanted to say thanks for helping out.

It looks like we've got something working now. (hopefully)

You're the best Rhino!
Logged
Matt Ashland, JRiver Media Center

RhinoBanga

  • Citizen of the Universe
  • *****
  • Posts: 1703
  • Developer
Re: Catching MM Keys (QUESTION)
« Reply #7 on: November 26, 2002, 06:13:24 am »

Cool ... glad to be of help.

Now hurry up and get that SDK finished :D :D :D
Logged
Pages: [1]   Go Up