Mutley2003
asked on
A single hotkey doing wakeup and global duty
hello there API gurus.
I am trying to find a solution to this. I want my app to be "woken up" by a hotkey (alt VK_SNAPSHOT) and then do its thing (screen capture and some manipulation), but I ALSO want that hotkey to have the same functionality if the app is running.
For the first part, I can use IShellLink::SetHotkey at installation time. For the second part I can use RegisterHotKey .. but I think there may be a conflict there.
I particularly want this "single keystroke" functionality .. the hotkey behaves the same whether the app is running or not (in which case the app is started).
Is there an elegant solution to this?
I am trying to find a solution to this. I want my app to be "woken up" by a hotkey (alt VK_SNAPSHOT) and then do its thing (screen capture and some manipulation), but I ALSO want that hotkey to have the same functionality if the app is running.
For the first part, I can use IShellLink::SetHotkey at installation time. For the second part I can use RegisterHotKey .. but I think there may be a conflict there.
I particularly want this "single keystroke" functionality .. the hotkey behaves the same whether the app is running or not (in which case the app is started).
Is there an elegant solution to this?
ASKER
Thanks SteH. The single instance idea is OK and I can do it with a mutex (see below)
Now, assume I have 2 shortcuts that differ only in their command line arguments.. this is so I can figure out which shortcut key was used to invoke the app (it is the same app in each).
Then I need to pass the command line argument through to the existing instance.
Any idea how I can do this?
const
cRandomString = 'RandomFred';
var
SingleMutex: THandle = 0;
procedure CreateMutexOrDie;
begin
if OpenMutex(MUTEX_ALL_ACCESS , False, cRandomString) = 0 then
begin
// First one - the mutex didn't exist, so create it.
SingleMutex := CreateMutex(nil, False, cRandomString);
end
else
begin
// The mutex did exist, so the application is running.
// Terminate it in this case.
Application.ShowMainForm := False;
Application.Terminate;
end;
end;
procedure FreeMutex;
begin
if SingleMutex <> 0 then
begin
CloseHandle(SingleMutex);
SingleMutex := 0;
end;
end;
initialization
CreateMutexOrDie;
finalization
FreeMutex;
end.
Now, assume I have 2 shortcuts that differ only in their command line arguments.. this is so I can figure out which shortcut key was used to invoke the app (it is the same app in each).
Then I need to pass the command line argument through to the existing instance.
Any idea how I can do this?
const
cRandomString = 'RandomFred';
var
SingleMutex: THandle = 0;
procedure CreateMutexOrDie;
begin
if OpenMutex(MUTEX_ALL_ACCESS
begin
// First one - the mutex didn't exist, so create it.
SingleMutex := CreateMutex(nil, False, cRandomString);
end
else
begin
// The mutex did exist, so the application is running.
// Terminate it in this case.
Application.ShowMainForm := False;
Application.Terminate;
end;
end;
procedure FreeMutex;
begin
if SingleMutex <> 0 then
begin
CloseHandle(SingleMutex);
SingleMutex := 0;
end;
end;
initialization
CreateMutexOrDie;
finalization
FreeMutex;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PS check syntax of the lines I added since I am not familiar anymore with pascal escpecially when it comes to windows API calls. But I think you will get the idea behind.
ASKER
Thanks SteH, I think the PostMessage solution will work just fine. I will need to find the handle to the window where the message is to be processed .. also I think I would show the first instance AFTER doing the postmessage. I wonder if SendMessage might be better .. so we wait for that to return before displaying the window of the first instance. Also I will need to handle the iconic situation, but that is discussed in the flounder article.
SendMessage has the problem that it waits for completion. Since you send it to a application you did yourself you can check whether a deadlock is possible. It shouldn't be in that case. It is for me a general style to use PostMessage unless I really need SendMessage in order not to assure that a deadlock is not possible.
See
www.flounder.com/nomultiples.htm
for more information.