//          KMP용 myHD리모콘 컨트롤러 프로그램~~
//          100%프리이므로 맘대로 수정해서 사용하세요~~

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, XPMan, Menus, RXShell, Registry, ShellAPI, HComPort;

type
  TMainForm = class(TForm)
    XPManifest1: TXPManifest;
    RxTrayIcon1: TRxTrayIcon;
    PopupMenu1: TPopupMenu;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
  private
    HSerialPort1: THSerialPort;
    FInited : Boolean;
    FLastString : string;
    FLastCnt : integer;
    FOldTick : DWORD;
    procedure HSerialPort1RxChar(Sender: TObject; ResvSize: Integer);
    procedure InitComport;
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses
 ShMem,
 wa_ipc; // Winamp 지원 유닛... KMP는 윈엠프 5.X API를 90%정도 지원하지만
         // 스킨 명령어 만으로 충분 하므로 사용하지 않는다.

const

// KMP Skin Button Command
 cMainMenu            = $0001;
 cSystemMenu          = $0002;
 cMinCmd              = $0003;
 cMaxCmd              = $0004;
 cExitCmd             = $0005;
 cOpenMenu            = $0006;
 cAlbumMenu           = $0007;
 cCloseCmd            = $0008;
 cWDMMenu             = $0009;
 cDVDMenu             = $000A;
 cWinampMenu          = $000B;
 cScreenMenu          = $000C;
 cAdvScreenMenu       = $000D;
 cPlaybackMenu        = $000E;
 cCaptionMenu         = $000F;
 cVideoMenu           = $0010;
 cAdvVideoMenu        = $0011;
 cAudioMenu           = $0012;
 cCaptureMenu         = $0013;
 cVisualMenu          = $0014;
 cFilterMenu          = $0015;
 cBookmarkMenu        = $0016;
 cEnvRestoreMenu      = $0017;
 cSkinMenu            = $0018;
 cOpenFileCmd         = $0019;
 cOpenURLCmd          = $001A;
 cOpenFolderCmd       = $001B;
 cOpenTVOutCmd        = $101B;
 cOpenIEMediaCmd      = $001C;
 cOpenRecentCmd       = $001D;
 cOpenReTryCmd        = $001E;
 cOpenWDMCmd          = $001F;
 cOpenBaseWDMCmd      = $101F;
 cOpenDVDDeviceCmd    = $0020;
 cOpenDVDFileCmd      = $0021;
 cOpenVCDCmd          = $0022;
 cAlbumPrevOpenCmd    = $0023;
 cAlbumNextOpenCmd    = $0024;
 cWDMTunerMagCmd      = $0025;
 cWDMChannelMagCmd    = $0026;
 cWDMFirstChannelCmd   = $0027;
 cWDMPrevChannelCmd    = $0028;
 cWDMNextChannelCmd    = $0029;
 cWDMLastChannelCmd    = $002A;
 cWDMRegPrevChannelCmd = $002B;
 cWDMRegNextChannelCmd = $002C;
 cWDMInputChannelCmd   = $002D;
 cWDMAntennaInputCmd   = $002E;
 cWDMCableInputCmd     = $002F;
 cDVDSubpictureMenu    = $0030;
 cDVDAudioMenu         = $0031;
 cDVDAngleMenu         = $0032;
 cDVDMoveMenu          = $0033;
 cDVDPrevChapterCmd    = $0034;
 cDVDNextChapterCmd    = $0035;
 cDVDRootMenuCmd       = $0036;
 cDVDTitleMenuCmd      = $0037;
 cDVDSubpictureMenuCmd = $0038;
 cDVDAudioMenuCmd      = $0039;
 cDVDAngleMenuCmd      = $003A;
 cDVDChapterMenuCmd    = $003B;
 cDVDRestoreMenuCmd    = $003C;
 cDVDClosedCaptionCmd  = $003D;
 cDVD1XBackwardCmd     = $003E;
 cDVD2XBackwardCmd     = $003F;
 cDVD4XBackwardCmd     = $0040;
 cDVD8XBackwardCmd     = $0041;
 cDVD1XForewardCmd     = $0042;
 cDVD2XForewardCmd     = $0043;
 cDVD4XForewardCmd     = $0044;
 cDVD8XForewardCmd     = $0045;
 cWinampPluginSetupCmd = $0046;
 cWinampPluginInfoCmd  = $0047;
 cWinampFileInfoCmd    = $0048;
 cScreenKeepBaseRatio  = $0049;
 cScreenKeep43Ratio    = $004A;
 cScreenKeep169Ratio   = $004B;
 cScreenKeep235Ratio   = $124B;
 cScreenKeepCurRatio   = $004C;
 cScreenCycleRatio     = $304C;
 cScreenMinSize        = $704D;
 cScreenVisOn          = $704E;
 cScreenVisOff         = $704F;
 cScreenHalfSize       = $004D;
 cScreenNormalSize     = $004E;
 cScreenDoubleSize     = $2046;
 cScreenMaxSize        = $2047;
 cScreenRestoreSize    = $3047;
 cScreenFullSize       = $2048;
 cScreenDesktopView    = $2166;
 cScreenWideSize       = $2266;
 cScreenTopLeft        = $2049;
 cScreenTopCenter      = $204A;
 cScreenTopRight       = $204B;
 cScreenMiddleLeft     = $204C;
 cScreenMiddleCenter   = $204D;
 cScreenMiddleRight    = $204E;
 cScreenBottomLeft     = $204F;
 cScreenBottomCenter   = $0050;
 cScreenBottomRight    = $0051;
 cScreenAllwaysOnTop   = $0052;
 cScreenPlayingOnTop   = $0053;
 cScreenScreenSave     = $0054;
 cScreenHideMouse      = $0055;
 cAdvScreenRestore     = $0056;
 cAdvScreenZoomIn      = $0057;
 cAdvScreenZoomOut     = $0058;
 cAdvScreenZoomInHori  = $0059;
 cAdvScreenZoomOutHori = $005A;
 cAdvScreenZoomInVert  = $005B;
 cAdvScreenZoomOutVert = $005C;
 cAdvScreenMoveLeft    = $005D;
 cAdvScreenMoveRight   = $005E;
 cAdvScreenMoveUp      = $005F;
 cAdvScreenMoveDown    = $0060;
 cAdvScreenOffset      = $0061;
 cPlayPlayPauseCmd     = $7061;
 cPlayPausePlayCmd     = $7062;
 cPlayPlayCmd          = $0062;
 cPlayStopCmd          = $0063;
 cPlayFrameCmd         = $0064;
 cPlayPrevFileCmd      = $0065;
 cPlayNextFileCmd      = $0066;
 cPlayBackward1Cmd     = $0067;
 cPlayForward1Cmd      = $0068;
 cPlayBackward2Cmd     = $0069;
 cPlayForward2Cmd      = $006A;
 cPlayBackward3Cmd     = $006B;
 cPlayForward3Cmd      = $006C;
 cPlayBackward4Cmd     = $006D;
 cPlayForward4Cmd      = $006E;
 cPlayPrevCapCmd       = $006F;
 cPlayNextCapCmd       = $0070;
 cPlayStartPosCmd      = $0071;
 cPlayMidPosCmd        = $0072;
 cPlayLastPosCmd       = $0073;
 cPlaySlowerSpeedCmd   = $0074;
 cPlayNormalSpeedCmd   = $0075;
 cPlayFasterSpeedCmd   = $0076;
 cPlaySlowerPitchCmd   = $0077;
 cPlayNormalPitchCmd   = $0078;
 cPlayFasterPitchCmd   = $0079;
 cPlayRepeatMenu       = $007A;
 cPlayRepeatSetCmd     = $307A;
 cPlayRepeatStartCmd   = $307B;
 cPlayRepeatEndCmd     = $307C;
 cPlayRepeatUseCmd     = $307D;
 cPlayModeMenu         = $007B;
 cPlayModeDirDown      = $307E;
 cPlayModeDirUp        = $307F;
 cPlayModeDirRandom    = $3080;
 cPlayModeDirRepeat    = $3081;
 cPlayModeAlbumNext    = $3082;
 cPlayModeAlbumRepeat  = $3083;
 cPlayModeAlbumNone    = $3084;
 cPlayModeAlbumExit    = $3085;
 cPlayModeAlbumPowerOff= $3086;
 cPlayModeAlbumFileClose= $3087;
 cPlayMoveMenu          = $3088;
 cPlaySkipStartCmd     = $3089;
 cPlaySkipIntroCmd     = $308C;
 cPlaySkipEndCmd       = $309A;
 cPlaySkipSettingCmd   = $309B;
 cPlaySaveFilePosCmd   = $309C;
 cPlayUseAVIKeyFrameCmd = $309D;
 cCaptionFileOpenCmd   = $007C;
 cCaptionVisibleCmd    = $007D;
 cCaptionSyncInputCmd  = $007E;
 cCaptionSyncPrevCmd   = $007F;
 cCaptionSyncNextCmd   = $0080;
 cCaptionOverlayCmd    = $0081;
 cCaptionImageCmd      = $0082;
 cCaptionAlignMenu     = $0083;
 cCaptionAlignLeftCmd  = $0084;
 cCaptionAlignCenterCmd= $0085;
 cCaptionAlignRightCmd = $0086;
 cCaptionAlignTopCmd    = $2084;
 cCaptionAlignMiddleCmd = $2085;
 cCaptionAlignBottomCmd = $2086;
 cCaptionLanguageMenu  = $0087;
 cCaptionMarginMenu    = $0088;
 cCaptionMargin0Cmd    = $0089;
 cCaptionMargin5Cmd    = $008A;
 cCaptionMargin10Cmd   = $008B;
 cCaptionMargin15Cmd   = $008C;
 cCaptionMargin20Cmd   = $008D;
 cCaptionMargin25Cmd   = $008E;
 cCaptionMargin30Cmd   = $008F;
 cCaptionLargerCmd     = $0090;
 cCaptionSmallerCmd    = $0091;
 cCaptionNormalCmd     = $0092;
 cCaptionBoldCmd       = $0093;
 cCaptionItialicCmd    = $0094;
 cCaptionAlphaCmd      = $0095;
 cCaptionUnderlineCmd  = $0096;
 cCaptionFadeCmd       = $0097;
 cCaptionShadowCmd     = $0098;
 cCaptionOutlineCmd    = $0099;
 cCaptionVertCmd       = $009A;
 cCaptionAntialiasCmd  = $009B;
 cCaptionHTMLCmd       = $009C;
 cCaptionMoveDownCmd   = $009D;
 cCaptionMoveUpCmd     = $009E;
 cCaptionIncMarginCmd  = $009F;
 cCaptionDecMarginCmd  = $1100;
 cVideoDecBaseBrightCmd= $1101;
 cVideoIncBaseBrightCmd= $1102;
 cVideoMotionBlurCmd   = $1103;
 cVideoLPFilterCmd     = $1104;
 cVideoSharpenFilterCmd= $1105;
 cVideoMediaBlockCmd   = $1106;
 cVideoMediaCrossCmd   = $1107;
 cVideoMeanYFilterCmd  = $1108;
 cVideoMeanUVFilterCmd = $1109;
 cVideoGreyScaleCmd    = $110A;
 cVideoAutoLevelCmd    = $110B;
 cVideoMirrorCmd       = $110C;
 cVideoInInverseCmd    = $110D;
 cVideoOutInverseCmd   = $110E;
 cVideoSpecialFilterMenu= $210F;
 cVideoHPFilterCmd     = $2110;
 cVideoLaplaceCmd      = $2101;
 cVideoEdgeDetectCmd   = $2102;
 cVideoEdgeEnhanceCmd  = $2103;
 cVideoColorEmbossCmd  = $2104;
 cVideoColorInverseCmd = $2105;
 cVideoHistogramCmd    = $2106;
 cVideoIgnoreSettingCmd= $2107;
 cVideoUsePluginCmd    = $2108;
 cAdvVideoLowUseOverSample = $3109;
 cAdvVideoAllUseOverSample = $310A;
 cAdvVideoNoUseOverSample  = $310B;
 cAdvVideoDeInterlaceCmd = $310C;
 cAdvVideoPostProcessCmd = $310D;
 cAdvVideoPicPropertyCmd = $310E;
 cAdvVideoSharpenCmd     = $310F;
 cAdvVideoBlurCmd        = $3110;
 cAdvVideoGDeNoiseCmd    = $3111;
 cAdvVideoLevelControlCmd= $3112;
 cAdvVideoFastestModeCmd = $3113;
 cAdvVideoFastPresetCmd  = $3114;
 cAdvVideoHQPresetCmd    = $3115;
 cAdvVideoBasePresetCmd  = $3116;
 cAdvVideoHardwareMenu   = $3117;
 cAdvVideoSoftwareMenu   = $3118;
 cAdvVideoHWIncBrightCmd = $3119;
 cAdvVideoHWBaseBrightCmd= $311A;
 cAdvVideoHWDecBrightCmd = $311B;
 cAdvVideoHWIncSaturatCmd = $311C;
 cAdvVideoHWBaseSaturatCmd= $311D;
 cAdvVideoHWDecSaturatCmd = $311E;
 cAdvVideoHWIncContrastCmd = $311F;
 cAdvVideoHWBaseContrastCmd= $3120;
 cAdvVideoHWDecContrastCmd = $3121;
 cAdvVideoSWIncBrightCmd = $3122;
 cAdvVideoSWBaseBrightCmd= $3123;
 cAdvVideoSWDecBrightCmd = $3124;
 cAdvVideoSWIncSaturatCmd = $3125;
 cAdvVideoSWBaseSaturatCmd= $3126;
 cAdvVideoSWDecSaturatCmd = $3127;
 cAdvVideoSWIncContrastCmd = $3128;
 cAdvVideoSWBaseContrastCmd= $3129;
 cAdvVideoSWDecContrastCmd = $312A;
 cAudioIncVolumeCmd        = $012C;
 cAudioDecVolumeCmd        = $012D;
 cAudioMuteVolumeCmd       = $012E;
 cAudioDecAmpCmd           = $012F;
 cAudioIncAmpCmd           = $0130;
 cAudioUseEqulizerCmd      = $0131;
 cAudioEqulizerPresetMenu  = $0132;
 cAudioEqulizerFreqDomain  = $2132;  // 추가 2.3.905...
 cAudioEqulizerTimeDomain  = $2133;  // 추가 2.3.905...
 cAudioEqulizerDirectX     = $2134;  // 추가 2.3.905...
 cAudioRemoveLeftChCmd     = $0133;
 cAudioRemoveRightChCmd    = $0134;
 cAudioSwapChCmd           = $0135;
 cAudioMergeChCmd          = $0136;
 cAudioVioceRemoveCmd      = $0137;
 cAudioViocePassCmd        = $0138;
 cAudioUseDynamicAmpCmd    = $2138; // 추가 2.3.905...
 cAudioUseAutoGainCmd      = $3310; // 추가 2.3.905...
 cAudioUseEchoCmd          = $2139; // 추가 2.3.905...
 cAudioSlowerEchoCmd       = $0139;
 cAudioFasterEchoCmd       = $013A;
 cAudioUseBandPassCmd      = $223A; // 추가 2.3.905...
 cAudioUseTrueBassCmd      = $223B; // 추가 2.3.905...
 cAudioUseTrebleEhnCmd     = $223C; // 추가 2.3.905...
 cAudioUseTempoCmd         = $023A;
 cAudioSlowerPitchCmd      = $014B;
 cAudioFasterPitchCmd      = $014C;
 cAudioUse3DEffectCmd      = $014D;
 cAudioDec3DEffectCmd      = $014E;
 cAudioInc3DEffectCmd      = $014F;
 cAudioIgnoreSettingCmd    = $0150;
 cAudioUsePluginCmd        = $0151;
 cCaptureAVIMovieCmd       = $0152;
 cCaptureAVIAutoCmd        = $0153;
 cCaptureCurScrFileCmd     = $0154;
 cCaptureCurScrTimeCmd     = $0155;
 cCaptureCurScrClipBoardCmd= $0156;
 cCaptureSampleGrabberCmd  = $0157;
 cCaptureDesktopCenterCmd  = $0158;
 cCaptureDesktopTileCmd    = $0159;
 cCaptureDesktopStretchCmd = $015A;
 cCaptureSelectFolderCmd   = $015B;
 cCaptureOpenFolderCmd     = $015C;
 cVisWaveFormCmd           = $015D;
 cVisFreqCmd               = $015E;
 cVisWaveFreqCmd           = $015F;
 cVisWinMediaVisCmd        = $0160;
 cVisSimpleCmd             = $0161;
 cVisBlackCmd              = $0162;
 cVisPrevVisCmd            = $0163;
 cVisNextVisCmd            = $0164;
 cBookmarkAddCmd           = $0165;
 cConfigureViewCmd         = $0167;
 cPlaylistViewCmd          = $0168;
 cControlBoxViewCmd        = $0169;
 cAdvMenuViewCmd           = $016A;
 cPlayInfoViewCmd          = $016B;
 cHelpViewCmd              = $016C;
 cAboutViewCmd             = $016D;
 cURLOpenCmd               = $016E;
 cSkinNormalControlSkin    = $016F;
 cSkinAutoHideControlSkin  = $0172;
 cSkinOSCControlSkin       = $0170;
 cSkinBothControlSkin      = $0171;
 cSkinChangeCmd            = $1171;

 cPlayListPopupMenu        = $0190;
 cPlayListAddFileCmd       = $0191;
 cPlayListAddFolderCmd     = $0192;
 cPlayListClearAllCmd      = $0193;

 cWDMChannel0Cmd           = $0294;
 cWDMChannel1Cmd           = $0295;
 cWDMChannel2Cmd           = $0296;
 cWDMChannel3Cmd           = $0297;
 cWDMChannel4Cmd           = $0298;
 cWDMChannel5Cmd           = $0299;
 cWDMChannel6Cmd           = $029A;
 cWDMChannel7Cmd           = $029B;
 cWDMChannel8Cmd           = $029C;
 cWDMChannel9Cmd           = $029D;
 cWDMChannelSetCmd         = $029E;

 cWinampMediaLibCmd        = $029F;
 cViewFileInfoCmd          = $0230;

 cSearchAnyFileCmd         = $0231;
// cNothingDoCmd             = $0232;

 cColorThemeMenu           = $0235;
 cColorThemeRandomCmd      = $0236;
 cColorThemeCycleCmd       = $0237;

 cDVDRelativeButton        = $210000;
 cDVDActivateButton        = $210001;
 cDVDGetSelectedButton     = $210002;

 cGetPlayModeCmd           = $220000; // 0 : Normal, 1 : DVD, 2 : WDM, 3 : Audio, 4: Video
 cGetIsPopupMenu           = $220001;

 cCaptureClipToAVICmd      = $024A;

 

 WM_SKINBTNMESSAGE         = WM_USER+1218;



 sRegistryKey = '\Software\KMPlayer\';
 sIdentfyString = 'KMP2.0';
 sOptionArea = 'OptionArea';
 sInstallPath = 'InstallPath';


function GetKMPPath : string;
var
 reg : TRegistry;
begin
 Result:='c:\Program Files\The KMPlayer\KMPlayer.exe';
 try
   reg:=TRegistry.Create;
   try
     reg.RootKey:=HKEY_CURRENT_USER;
     if reg.OpenKey(sRegistryKey+sIdentfyString+'\'+sOptionArea,False) then begin
       try
         if reg.ValueExists(sInstallPath) then Result:=reg.ReadString(sInstallPath);
       except
       end;
       reg.CloseKey;
     end;
   finally
     reg.Free;
   end;
 except
 end;
end;

function GetKMPHandle : THandle;
begin
 with TSharedMem.Create(Application) do begin
   MapName:=sIdentfyString;
   Active:=True;
   Result:=pInteger(Buffer+4)^;
   if IsWindow(Result)=False then Result:=0;
   Free;
 end;
end;

{ TMainForm }

procedure Gen_KeyEvent(aa : integer);
var
 dx,dy,flag : integer;
begin
 dx:=(aa shr 16) and $ff;
 dy:=aa; // virtual key
 flag:=0;
 if (aa and (1 shl 24)<>0) then flag:=KEYEVENTF_EXTENDEDKEY;
 if (aa and (1 shl 31)<>0) then flag:=flag or KEYEVENTF_KEYUP;
 keybd_event(dy,dx,flag,0);
end;

procedure TMainForm.HSerialPort1RxChar(Sender: TObject; ResvSize: Integer);
var
 str : string;
 han : THandle;
 cmd,mode,selected : integer;
 isActive : Boolean;
const
 DVD_Relative_Upper = 1;
 DVD_Relative_Lower = 2;
 DVD_Relative_Left  = 3;
 DVD_Relative_Right = 4;
begin
 HSerialPort1.ReadString(str,ResvSize);
 if Pos('OK',str)>0 then FInited:=True;
 han:=GetKMPHandle;

 if str=#1#0#0#0#0#0 then begin
   inc(FLastCnt);
   if FLastCnt>=4 then str:=FLastString;
 end;
 if(GetTickCount-FOldTick)>200 then FLastCnt:=0;
 FOldTick:=GetTickCount;

 if (ResvSize=6) and (str[1]='0') then begin
   if FLastString<>str then FLastCnt:=0;
   FLastString:=str;
   cmd:=Byte(str[3]);
   mode:=SendMessage(han,WM_SKINBTNMESSAGE,cGetPlayModeCmd,0); // 0 : Normal, 1 : DVD, 2 : WDM, 3 : Audio, 4: Video
   isActive:=(han=GetForegroundWindow); // 활성화된 창인지 검사
   if isActive then isActive:=SendMessage(han,WM_SKINBTNMESSAGE,cGetIsPopupMenu,0)=0; // 메뉴가 펼쳐져 있는지 검사
   selected:=SendMessage(han,WM_SKINBTNMESSAGE,cDVDGetSelectedButton,0); // 0보다 크면 DVD Menu 존재
   case cmd of // 버턴에 따라서 처리~~
     0 : begin
           if han=0 then begin
             str:=GetKMPPath;
             ShellExecute(0,'open',pchar(str),'','',SW_SHOW);
           end
           else PostMessage(han,WM_SKINBTNMESSAGE,cExitCmd,0);
         end;
     1 : if mode=2 then PostMessage(han,WM_SKINBTNMESSAGE,cWDMChannelSetCmd,0)
         else if mode=0 then PostMessage(han,WM_SKINBTNMESSAGE,cOpenRecentCmd,0);
     2..9 : begin
              if mode=0 then begin
                if cmd=2 then PostMessage(han,WM_SKINBTNMESSAGE,cOpenTVOutCmd,0)
                else if cmd=3 then PostMessage(han,WM_SKINBTNMESSAGE,cOpenDVDDeviceCmd,0)
                else if cmd=4 then PostMessage(han,WM_SKINBTNMESSAGE,cOpenBaseWDMCmd,0);
              end
              else if mode=2 then PostMessage(han,WM_SKINBTNMESSAGE,cWDMChannel1Cmd+cmd-2,0)
              else Gen_KeyEvent(Byte('0')+cmd-1);
            end;
     16 : if mode=2 then PostMessage(han,WM_SKINBTNMESSAGE,cWDMChannel9Cmd,0)
          else PostMessage(han,WM_SKINBTNMESSAGE,cControlBoxViewCmd,0);
     17 : if mode=2 then PostMessage(han,WM_SKINBTNMESSAGE,cWDMChannel0Cmd,0)
          else PostMessage(han,WM_SKINBTNMESSAGE,cPlaylistViewCmd,0);
     18 : if selected>0 then PostMessage(han,WM_SKINBTNMESSAGE,cDVDRelativeButton,DVD_Relative_Upper)
          else if (mode=2) and isActive then Gen_KeyEvent(VK_PRIOR)
          else if isActive then Gen_KeyEvent(VK_RIGHT)
          else Gen_KeyEvent(VK_UP);
     19 : if (selected>0) then PostMessage(han,WM_SKINBTNMESSAGE,cDVDRelativeButton,DVD_Relative_Left)
          else if isActive then PostMessage(han,WM_SKINBTNMESSAGE,cAudioDecVolumeCmd,0)
          else Gen_KeyEvent(VK_LEFT);
     20 : if selected>0 then SendMessage(han,WM_SKINBTNMESSAGE,cDVDActivateButton,0)
          else Gen_KeyEvent(VK_RETURN);
     21 : if selected>0 then PostMessage(han,WM_SKINBTNMESSAGE,cDVDRelativeButton,DVD_Relative_Right)
          else if isActive then PostMessage(han,WM_SKINBTNMESSAGE,cAudioIncVolumeCmd,0)
          else Gen_KeyEvent(VK_RIGHT);
     22 : if selected>0 then SendMessage(han,WM_SKINBTNMESSAGE,cDVDRelativeButton,DVD_Relative_Lower)
          else if (mode=2) and isActive then Gen_KeyEvent(VK_NEXT)
          else if isActive then Gen_KeyEvent(VK_LEFT)
          else Gen_KeyEvent(VK_DOWN);
     23 : if mode=1 then PostMessage(han,WM_SKINBTNMESSAGE,cDVDAudioMenuCmd,0)
          else PostMessage(han,WM_SKINBTNMESSAGE,cAudioMenu,0);
     24 : PostMessage(han,WM_SKINBTNMESSAGE,cAudioMuteVolumeCmd,0);
     25 : if (mode=1) and isActive then PostMessage(han,WM_SKINBTNMESSAGE,cDVDSubpictureMenu,0)
          else Gen_KeyEvent(VK_TAB);
     32 : PostMessage(han,WM_SKINBTNMESSAGE,cDVDChapterMenuCmd,0);
     33 : if mode=1 then PostMessage(han,WM_SKINBTNMESSAGE,cDVDTitleMenuCmd,0)
          else PostMessage(han,WM_SKINBTNMESSAGE,cMainMenu,0);
     34 : if mode=1 then PostMessage(han,WM_SKINBTNMESSAGE,cDVDRestoreMenuCmd,0)
          else begin
            Gen_KeyEvent(VK_ESCAPE);
          end;
     35 : PostMessage(han,WM_SKINBTNMESSAGE,cOpenBaseWDMCmd,0);
     36 : PostMessage(han,WM_SKINBTNMESSAGE,cScreenCycleRatio,0);
     37 : PostMessage(han,WM_SKINBTNMESSAGE,cCaptionVisibleCmd,0);
     38 : PostMessage(han,WM_SKINBTNMESSAGE,cPlayBackward2Cmd,0);
     39 : PostMessage(han,WM_SKINBTNMESSAGE,cPlayPlayCmd,0);
     40 : PostMessage(han,WM_SKINBTNMESSAGE,cPlayForward2Cmd,0);
     41 : PostMessage(han,WM_SKINBTNMESSAGE,cPlayPrevFileCmd,0);
     48 : PostMessage(han,WM_SKINBTNMESSAGE,cPlayStopCmd,0);
     49 : PostMessage(han,WM_SKINBTNMESSAGE,cPlayNextFileCmd,0);
     50 : PostMessage(han,WM_SKINBTNMESSAGE,cScreenMaxSize,0);
     51 : if mode=2 then PostMessage(han,WM_SKINBTNMESSAGE,cCaptureAVIMovieCmd,0)
          else PostMessage(han,WM_SKINBTNMESSAGE,cCaptureClipToAVICmd,0);
   end;
 end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
 FLastCnt:=0;
 FOldTick:=0;
 HSerialPort1:=THSerialPort.Create(Self);
 HSerialPort1.OnRxChar:=HSerialPort1RxChar;
 InitComport;
end;

procedure TMainForm.InitComport;
var
 i,j : integer;
begin
 FInited:=False;
 for i:=0 to 3 do begin
   HSerialPort1.Close;
   HSerialPort1.Port:=TPortType(i);
   HSerialPort1.Open;
   if HSerialPort1.GetComHandle<>INVALID_HANDLE_VALUE then begin
     Sleep(200);
     HSerialPort1.WriteString('I');
     HSerialPort1.WriteString('R');
     j:=0;
     while j<100 do begin
       if FInited then break;
       Sleep(10);
       Application.ProcessMessages;
       inc(j);
     end;
     if FInited then break;
   end;
 end;
end;

procedure TMainForm.N4Click(Sender: TObject);
begin
 InitComport;
end;

procedure TMainForm.N6Click(Sender: TObject);
begin
 Application.Terminate;
end;

end.
