Mega Code Archive

 
Categories / Delphi / Hardware
 

How to catch events from MS Intellitype Keyboards

Title: How to catch events from MS Intellitype Keyboards Question: Anybody out there like myself who own new keyboards that have special multimedia function buttons on them want to be able to use those buttons in their applications. Answer: Basically the keyboard events are fired in the Windows API only after you install the Intellitype software from Microsoft. These new messages are part of the Win XP/ME/2000 operating systems, but if you own Win 95/98/NT these messages are only fired if you install Intellitype software. Basically you need to trap these messages in your Main Form's WndProc message handler. unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public { Public declarations } procedure WndProc(var Message: TMessage); override; end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.WndProc(var Message: TMessage); const WM_APPCOMMAND = $319; WM_APPCOMMAND_MEDIA_NEXTTRACK = 11; //Go to next track. WM_APPCOMMAND_MEDIA_PREVIOUSTRACK = 12; //Go to previous track. WM_APPCOMMAND_MEDIA_STOP = 13; //Stop playback. WM_APPCOMMAND_MEDIA_PLAY_PAUSE = 14; //Play or pause playback. var Handled : boolean; begin with Mesg do begin if (Msg = WM_APPCOMMAND) then begin { Process the command } case (LParamHi and $FFF) of WM_APPCOMMAND_MEDIA_STOP: begin frmNowPlaying.Stop(); Handled := TRUE; end; WM_APPCOMMAND_MEDIA_PLAY_PAUSE: begin if FPlayerStatus = psPlay then frmNowPlaying.Pause() else frmNowPlaying.Play(); Handled := TRUE; end; WM_APPCOMMAND_MEDIA_PREVIOUSTRACK: begin frmNowPlaying.Previous(); Handled := TRUE; end; WM_APPCOMMAND_MEDIA_NEXTTRACK: begin frmNowPlaying.Next(); Handled := TRUE; end; else { Unknown command - not handled here so bubble up the message Q } Handled := FALSE; end; Result := LongInt(Handled); end else { Not WM_APPCOMMAND - we're not interested } inherited; end; {with} end; end. Now that we are trapping for the messages and responding to them, you might want to register with the Keyboard to let it know your app is watching. This really isn't necessary but this will make your app appear in the Media Source window when you press the special "Media" button on your keyboard. This feature is only for the Intellitype software version 1.1 or lower as the Media key in Intellitype 2+ software just opens the default Media Player. Just call this function below passing the name you want displayed and the full path to your EXE. {=============================================================================== :Procedure: RegisterIntellitypeProMedia :Author: Emil Lefkof :Date Created: 02/06/2001 :Description: Register an application for Intellitype Pro keyboards. :Inputs: strDisplayName(string): the name used for display strEXEPath(string): exe location "C:\myapp.exe" Modification History ----------------------------------------------------------- Date Init Description ---- ---- -------------------------------------------------------------------- 02/06/2001 EAL Initial Version ===============================================================================} procedure RegisterIntellitypeProMedia(strDisplayName, strEXEPath:string); const MSKB_KEY = '\Software\Microsoft\Keyboard\Native Media Players'; var reg:TRegistry; begin reg:=TRegistry.Create; try reg.RootKey:=HKEY_CURRENT_USER; {check if the Media Player Key exists, if not create it} reg.OpenKey(MSKB_KEY,True); {create the new key for the app} reg.OpenKey(MSKB_KEY+'\'+strDisplayName,True); //create a new value for this key -- pcifile reg.WriteString('AppName',strDisplayName); reg.WriteString('ExePath',strEXEPath); reg.CloseKey; finally reg.Free; end; end; That's it, you software is now taking full advantage of your cool keyboard!! IF you want to do it even easier than this check out Carbonsoft's great FREE componenet that wraps this code for you. Check out http://www.carbonsoft.com Emil Lefkof Melman Consulting Group emil@melmangroup.com