Skip to content

Commit 58621b7

Browse files
committed
Add more codes
1 parent e57816d commit 58621b7

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

Flow.Launcher.Infrastructure/NativeMethods.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,11 @@ IWebBrowser2
6969
EVENT_OBJECT_DESTROY
7070
EVENT_OBJECT_LOCATIONCHANGE
7171
EVENT_SYSTEM_MOVESIZESTART
72-
EVENT_SYSTEM_MOVESIZEEND
72+
EVENT_SYSTEM_MOVESIZEEND
73+
GetFocus
74+
SetFocus
75+
MapVirtualKey
76+
WM_KEYUP
77+
WM_KEYDOWN
78+
GetCurrentThreadId
79+
AttachThreadInput

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ internal static bool DirJump(string dirPath, HWND dialogHandle, bool altD = true
708708
return DirFileJump(dirPath, null, dialogHandle, altD);
709709
}
710710

711-
private static bool DirFileJump(string dirPath, string filePath, HWND dialogHandle, bool altD = true, bool editFileName = false)
711+
private static unsafe bool DirFileJump(string dirPath, string filePath, HWND dialogHandle, bool altD = true, bool editFileName = false)
712712
{
713713
// Directly edit file name input box.
714714
if (editFileName)
@@ -719,12 +719,26 @@ private static bool DirFileJump(string dirPath, string filePath, HWND dialogHand
719719
// Alt-D or Ctrl-L to focus on the path input box
720720
if (altD)
721721
{
722-
_inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, VirtualKeyCode.VK_D);
722+
_inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LMENU, VirtualKeyCode.VK_D);
723723
}
724724
else
725725
{
726726
_inputSimulator.Keyboard.ModifiedKeyStroke(VirtualKeyCode.LCONTROL, VirtualKeyCode.VK_L);
727727
}
728+
/*if (altD)
729+
{
730+
SendKey(dialogHandle, VIRTUAL_KEY.VK_LMENU, false); // Press Left Alt
731+
SendKey(dialogHandle, VIRTUAL_KEY.VK_D, false); // Press D
732+
SendKey(dialogHandle, VIRTUAL_KEY.VK_D, true); // Release D
733+
SendKey(dialogHandle, VIRTUAL_KEY.VK_LMENU, true); // Release Left Alt
734+
}
735+
else
736+
{
737+
SendKey(dialogHandle, VIRTUAL_KEY.VK_LCONTROL, false); // Press Left Ctrl
738+
SendKey(dialogHandle, VIRTUAL_KEY.VK_L, false); // Press L
739+
SendKey(dialogHandle, VIRTUAL_KEY.VK_L, true); // Release L
740+
SendKey(dialogHandle, VIRTUAL_KEY.VK_LCONTROL, true); // Release Left Ctrl
741+
}*/
728742

729743
// Get the handle of the path input box and then set the text.
730744
// The window with class name "ComboBoxEx32" is not visible when the path input box is not with the keyboard focus.
@@ -742,7 +756,7 @@ private static bool DirFileJump(string dirPath, string filePath, HWND dialogHand
742756

743757
var timeOut = !SpinWait.SpinUntil(() =>
744758
{
745-
int style = PInvoke.GetWindowLong(controlHandle, WINDOW_LONG_PTR_INDEX.GWL_STYLE);
759+
var style = PInvoke.GetWindowLong(controlHandle, WINDOW_LONG_PTR_INDEX.GWL_STYLE);
746760
return (style & (int)WINDOW_STYLE.WS_VISIBLE) != 0;
747761
}, 1000);
748762
if (timeOut)
@@ -757,6 +771,19 @@ private static bool DirFileJump(string dirPath, string filePath, HWND dialogHand
757771
return false;
758772
}
759773

774+
/*var dwMyID = PInvoke.GetCurrentThreadId();
775+
var dwCurID = PInvoke.GetWindowThreadProcessId(dialogHandle);
776+
777+
PInvoke.AttachThreadInput(dwMyID, dwCurID, true);
778+
779+
var timeOut1 = !SpinWait.SpinUntil(() => PInvoke.GetFocus() == editHandle, 1000);
780+
if (timeOut1)
781+
{
782+
return false;
783+
}
784+
785+
PInvoke.AttachThreadInput(dwMyID, dwCurID, false);*/
786+
760787
SetWindowText(editHandle, dirPath);
761788
_inputSimulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);
762789

@@ -820,6 +847,43 @@ public static unsafe bool GetWindowRect(nint handle, out Rect outRect)
820847
return true;
821848
}
822849

850+
private static void SendKey(HWND hWnd, VIRTUAL_KEY virtualKey, bool isKeyUp)
851+
{
852+
// Get virtual key value
853+
var virtualKeyValue = (ushort)virtualKey;
854+
855+
// Get scan code and extended flag
856+
var scanCode = PInvoke.MapVirtualKey(virtualKeyValue, MAP_VIRTUAL_KEY_TYPE.MAPVK_VK_TO_VSC);
857+
858+
// Check if the key is an extended key (e.g., right Alt/Ctrl)
859+
var isExtended = virtualKey == VIRTUAL_KEY.VK_RMENU || virtualKey == VIRTUAL_KEY.VK_RCONTROL;
860+
861+
// Create lParam
862+
var lParam = CreateKeyLParam(scanCode, isExtended, isKeyUp, !isKeyUp);
863+
864+
// Send message
865+
var message = isKeyUp ? PInvoke.WM_KEYUP : PInvoke.WM_KEYDOWN;
866+
PInvoke.PostMessage(hWnd, message, virtualKeyValue, new(lParam));
867+
}
868+
869+
private static nint CreateKeyLParam(uint scanCode, bool isExtended, bool isKeyUp, bool wasKeyDown)
870+
{
871+
uint lParam = 0x00000001; // Repeat count (1 keystroke)
872+
873+
lParam |= (scanCode << 16); // Scan code
874+
875+
if (isExtended)
876+
lParam |= 0x01000000; // Extended key flag
877+
878+
if (wasKeyDown)
879+
lParam |= 0x40000000; // Previous key state (1 if down before message)
880+
881+
if (isKeyUp)
882+
lParam |= 0x80000000; // Transition state (1 for release)
883+
884+
return (nint)lParam;
885+
}
886+
823887
#endregion
824888
}
825889
}

0 commit comments

Comments
 (0)