Skip to content

Fix: Implement Ctrl+C for selected text in Output Window #2681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2017</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2018</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2019</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2020</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2021</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2022</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,14 @@
<RevitVersion>2023</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@
<RevitVersion>2024</RevitVersion>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
</Project>

<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@
<RevitVersion>2025</RevitVersion>
<TargetFramework>net8.0-windows</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@
<RevitVersion>2026</RevitVersion>
<TargetFramework>net8.0-windows</TargetFramework>
</PropertyGroup>
</Project>
<ItemGroup>
<COMReference Include="MSHTML">
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
<VersionMajor>4</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>primary</WrapperTool>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>
</Project>
42 changes: 42 additions & 0 deletions dev/pyRevitLabs.PyRevit.Runtime/ScriptConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Autodesk.Revit.UI;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Windows.Input; // Added for KeyEventArgs and ModifierKeys
using mshtml; // Added for IHTMLDocument2 - assuming COM reference is available

using pyRevitLabs.Common;
using pyRevitLabs.CommonWPF.Controls;
Expand Down Expand Up @@ -346,6 +348,46 @@ public void InitializeComponent() {
this.Deactivated += ScriptOutput_LostFocus;

this.OutputTitle = PyRevitLabsConsts.ProductName;

// Register PreviewKeyDown event handler
this.PreviewKeyDown += ScriptConsole_PreviewKeyDown;
}

private void ScriptConsole_PreviewKeyDown(object sender, KeyEventArgs e)
{
// Check for Ctrl+C
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
// Check if the WebBrowser control (renderer) or a child of it has focus
if (renderer != null && renderer.IsKeyboardFocusWithin)
{
try
{
// Attempt to get selected text using IHTMLDocument2
IHTMLDocument2 htmlDocument = renderer.Document.DomDocument as IHTMLDocument2;
if (htmlDocument != null)
{
IHTMLSelectionObject currentSelection = htmlDocument.selection;
if (currentSelection != null)
{
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null && !string.IsNullOrEmpty(range.text))
{
Clipboard.SetText(range.text);
e.Handled = true; // Mark event as handled to prevent further processing
}
}
}
}
catch (Exception /*ex*/)
{
// Log or handle the exception if necessary, or ignore if it's minor
// For now, we can ignore it if direct text retrieval fails,
// as the default WebBrowser Ctrl+C might still work.
// Consider adding a debug log here: e.g., Debug.WriteLine($"Ctrl+C handler error: {ex.Message}");
}
}
}
}

[System.Diagnostics.DebuggerNonUserCodeAttribute()]
Expand Down