diff --git a/windows/src/desktop/kmshell/main/initprog.pas b/windows/src/desktop/kmshell/main/initprog.pas index 75495dc78e..deab8329f7 100644 --- a/windows/src/desktop/kmshell/main/initprog.pas +++ b/windows/src/desktop/kmshell/main/initprog.pas @@ -376,7 +376,7 @@ begin { Run app reconfiguration tasks } - TKeymanStartTask.Recreate; + TKeymanStartTask.RecreateTask; UILanguages.CreateUILanguages; diff --git a/windows/src/desktop/kmshell/util/Keyman.System.KeymanStartTask.pas b/windows/src/desktop/kmshell/util/Keyman.System.KeymanStartTask.pas index 28203d730e..c4010757b1 100644 --- a/windows/src/desktop/kmshell/util/Keyman.System.KeymanStartTask.pas +++ b/windows/src/desktop/kmshell/util/Keyman.System.KeymanStartTask.pas @@ -3,10 +3,12 @@ unit Keyman.System.KeymanStartTask; interface type + /// Create or recreate the automatic task to start Keyman if a TIP + /// is selected based on an event in the Event Log TKeymanStartTask = class private public - class procedure Recreate; + class procedure RecreateTask; class procedure DeleteTask; class procedure CreateTask; end; @@ -28,12 +30,18 @@ uses const CTaskName = 'Start On Demand'; + CTaskDescription = 'Starts Keyman on demand when a Keyman input method is selected'; + CTaskAuthor = 'SIL International'; + CTaskFolderName = 'Keyman'; CTaskTrigger = ''+ ''+ ''+ ''+ ''; + CTaskEventTriggerId = 'Keyman_StartOnDemand'; + + CKeymanExeStartArguments = '-kmc start'; class procedure TKeymanStartTask.CreateTask; var @@ -47,35 +55,37 @@ var begin pService := CoTaskScheduler_.Create; pService.Connect(EmptyParam, EmptyParam, EmptyParam, EmptyParam); + + // Create or open the Keyman task folder try - pTaskFolder := pService.GetFolder('\Keyman'); + pTaskFolder := pService.GetFolder('\' + CTaskFolderName); except - pTaskFolder := pService.GetFolder('\').CreateFolder('Keyman', EmptyParam); + pTaskFolder := pService.GetFolder('\').CreateFolder(CTaskFolderName, EmptyParam); end; + // Create a new task pTask := pService.NewTask(0); - pService := nil; - // TODO: create a Keyman folder pRegInfo := pTask.RegistrationInfo; - pRegInfo.Author := 'SIL International'; - pRegInfo.Description := 'Starts Keyman on demand when a Keyman input method is selected'; + pRegInfo.Author := CTaskAuthor; + pRegInfo.Description := CTaskDescription; pRegInfo.Version := CKeymanVersionInfo.VersionWithTag; - pRegInfo := nil; pSettings := pTask.Settings; pSettings.DisallowStartIfOnBatteries := False; pSettings.StopIfGoingOnBatteries := False; - pSettings.ExecutionTimeLimit := 'PT0S'; + pSettings.ExecutionTimeLimit := 'PT0S'; // no time limit pSettings.Priority := 4; // https://github.com/microsoft/WinDev/issues/55 + // Start the task when Keyman event 256 is logged pEventTrigger := pTask.Triggers.Create(TASK_TRIGGER_EVENT) as IEventTrigger; - pEventTrigger.Id := 'Keyman_StartOnDemand'; + pEventTrigger.Id := CTaskEventTriggerId; pEventTrigger.Subscription := CTaskTrigger; + // Action is to run keyman.exe as login user pAction := pTask.Actions.Create(TASK_ACTION_EXEC) as IExecAction; pAction.Path := TKeymanPaths.KeymanEngineInstallPath(TKeymanPaths.S_KeymanExe); - pAction.Arguments := '-kmc start'; + pAction.Arguments := CKeymanExeStartArguments; pTaskFolder.RegisterTaskDefinition(CTaskName, pTask, TASK_CREATE_OR_UPDATE or TASK_IGNORE_REGISTRATION_TRIGGERS, EmptyParam, EmptyParam, TASK_LOGON_NONE, EmptyParam); @@ -89,9 +99,9 @@ begin pService := CoTaskScheduler_.Create; pService.Connect(EmptyParam, EmptyParam, EmptyParam, EmptyParam); try - pTaskFolder := pService.GetFolder('\Keyman'); + pTaskFolder := pService.GetFolder('\'+CTaskFolderName); except - // Assume that the folder doesn't exist + // Assume that the folder doesn't exist, nothing to do Exit; end; @@ -100,17 +110,16 @@ begin except // We'll assume that the task doesn't exist, and continue end; + if pTaskFolder.GetTasks(0).Count = 0 then begin pTaskFolder := nil; - pService.GetFolder('\').DeleteFolder('Keyman', 0); + pService.GetFolder('\').DeleteFolder(CTaskFolderName, 0); end; end; -class procedure TKeymanStartTask.Recreate; +class procedure TKeymanStartTask.RecreateTask; begin - // Create or recreate the automatic task to start Keyman if a TIP is selected - // based on an event in the Event Log if not Reg_GetDebugFlag(SRegValue_Flag_UseAutoStartTask, True) then begin DeleteTask;