Launch with key input

48 views
Skip to first unread message

nao doc

unread,
Aug 1, 2023, 8:00:04 PM8/1/23
to radstu...@googlegroups.com

こんにちは、naoです。
windowsでエクスプローラーからダブルクリックしたり、radstudioの”ツール”のプルダウンメニューから
起動したりできるプログラムを作っています。
起動時にキー(例えばLeft Control)を押して、初期動作ないし挙動を変化させるような
仕組みを実現すべく、formのコンストラクタで以下のようにしてキーを検出を目論んでいます。

bool controlled = false;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
if( GetKeyState(VK_LCONTROL) & 0x8000 ){
Label1->Caption = "Left Control";
controlled = true;
}
}

キーを押しながら起動すると、検出はできますが、起動したプログラムが背面に隠れてしまい、
formのどこかをクリックしないとactiveになりません。win32のapiを色々試してみましたが、
うまくいきません。何かやり方はないでしょうか?情報をいただければ幸いです。



--
nao <jake....@gmail.com>

nao

unread,
Aug 13, 2023, 12:36:26 AM8/13/23
to Japan RAD Studio User Group

いわゆる自己レス(死語か、歳がばれるか)ですが、
他所やらでSetForegroundWindowを使うのだというヒントが
もらえましたが、そのままでは動かず。


のSetForegroundWindowEx();
を使って、所望の動作が得られました。余計なことをやるべくやらない方法です。
SetForegroundWindowが動かないという例は多々あるようで、
ForegroundLockTimeout の値をregistry editorで恒久的にいじったり
プログラム的に一時的にゼロにしたりの手もあるようですね。以下で
一応動いています。   



void SetForegroundWindowEx(HWND hWnd)


{


int targetThread, selfThread;




// Threadのインプットのアタッチ


targetThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);


selfThread = GetCurrentThreadId();


AttachThreadInput(selfThread, targetThread, TRUE );




// ウィンドウをフォアグラウンドに持ってくる


SetForegroundWindow(hWnd);




// Threadを切り離す


AttachThreadInput(selfThread, targetThread, FALSE );


}


//---------------------------------------------------------------------------


__fastcall TForm1::TForm1(TComponent* Owner)


: TForm(Owner)


{


if (GetKeyState(VK_LCONTROL) & 0x8000) {


Label1->Caption = "Left Control";


controlled = true;




// Bring the form to the foreground and activate it


SetForegroundWindowEx(Handle);






}






}



2023年8月2日水曜日 9:00:04 UTC+9 nao doc:
Reply all
Reply to author
Forward
0 new messages