Send key implementation without locate value#4315
Conversation
WalkthroughAdded a private P/Invoke for user32.GetForegroundWindow and updated ActSendKeys.Execute: when LocateValueCalculated is empty the method logs a debug message, queries the foreground window, and sends keystrokes directly to the focused window (using SendKeysSlowly or SendKeys) then returns. Existing locate-by-title/class flows remain unchanged; no public API signature changes. Changes
Sequence Diagram(s)sequenceDiagram
actor User
participant Act as ActSendKeys.Execute
participant U32 as user32.GetForegroundWindow
participant Target as Focused Window
participant Finder as Locator (Title/Class)
User->>Act: Execute()
alt LocateValueCalculated provided
Act->>Finder: Locate window (ByTitle / ByClassName)
Finder-->>Act: Found / Not found
Act->>Target: SendKeys / SendKeysSlowly
Act-->>User: Done
else LocateValueCalculated empty
Act->>Act: Log debug "sending to focused window"
Act->>U32: GetForegroundWindow()
U32-->>Act: HWND (0 or non-zero)
alt HWND == 0
Act-->>User: Error: no focused window
else HWND != 0
Act->>Target: SendKeys / SendKeysSlowly
Act-->>User: Done
end
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
Ginger/GingerCore/Actions/ActSendKeys.cs (1)
178-184: Dead code: Validation will never trigger.After the logic fix suggested in the previous comment, the validation on line 179 becomes unreachable. Here's why:
- If
LocateValueCalculatedis empty, the code returns early (line 168 in current implementation)- If
LocateValueCalculatedis NOT empty, execution reaches line 178- Line 178 assigns
titleFromUser = LocateValueCalculated- Line 179 checks
if (string.IsNullOrEmpty(titleFromUser)), but this can never be true since we knowLocateValueCalculatedis NOT empty at this pointApply this diff to remove the dead code:
//locate value String titleFromUser = LocateValueCalculated; - if (string.IsNullOrEmpty(titleFromUser)) - { - Error = "Missing window Locate Value."; - return; - - }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
Ginger/GingerCore/Actions/ActSendKeys.cs(1 hunks)
🔇 Additional comments (1)
Ginger/GingerCore/Actions/ActSendKeys.cs (1)
141-142: LGTM! P/Invoke declaration is correct.The GetForegroundWindow declaration follows standard patterns and is consistent with other P/Invoke declarations in this file.
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
Ginger/GingerCore/Actions/ActSendKeys.cs (1)
169-176: Remove unreachable validation.The validation block at lines 169-176 contains unreachable code. The check
if (string.IsNullOrEmpty(titleFromUser))at line 171 will never be true because:
- If
LocateValueCalculatedis empty, the code returns at line 161- If
LocateValueCalculatedis not empty,titleFromUseris assigned the same non-empty value at line 170The empty-string check is now redundant and can be removed.
Apply this diff to remove the unreachable validation:
//locate value String titleFromUser = LocateValueCalculated; - if (string.IsNullOrEmpty(titleFromUser)) - { - Error = "Missing window Locate Value."; - return; - - } } catch (Exception e)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
Ginger/GingerCore/Actions/ActSendKeys.cs(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
Ginger/GingerCore/Actions/ActSendKeys.cs (1)
Ginger/GingerCoreCommon/ReporterLib/Reporter.cs (1)
Reporter(28-357)
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
Ginger/GingerCore/Actions/ActSendKeys.cs(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-01T06:43:01.042Z
Learnt from: prashelke
PR: Ginger-Automation/Ginger#4315
File: Ginger/GingerCore/Actions/ActSendKeys.cs:149-161
Timestamp: 2025-10-01T06:43:01.042Z
Learning: In ActSendKeys.cs, when LocateValueCalculated is empty, the action should send keys blindly to the current focus without validating whether a window is focused (i.e., without checking if GetForegroundWindow() returns IntPtr.Zero). This is the intended behavior to support scenarios where keys need to be sent regardless of window focus state.
Applied to files:
Ginger/GingerCore/Actions/ActSendKeys.cs
🧬 Code graph analysis (1)
Ginger/GingerCore/Actions/ActSendKeys.cs (1)
Ginger/GingerCoreCommon/ReporterLib/Reporter.cs (1)
Reporter(28-357)
Thank you for your contribution.
Before submitting this PR, please make sure:
Summary by CodeRabbit
New Features
Bug Fixes