-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0def2e1
commit 7a8b2dc
Showing
13 changed files
with
627 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.vs | ||
CursorLocker.sln.DotSettings.user | ||
CursorLocker/obj | ||
CursorLocker/bin | ||
CursorLocker/Connected Services |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.26730.16 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CursorLocker", "CursorLocker\CursorLocker.csproj", "{305177F7-5FAE-4BA3-BD41-40DB58C94C68}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Debug|x64 = Debug|x64 | ||
Release|Any CPU = Release|Any CPU | ||
Release|x64 = Release|x64 | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Debug|x64.ActiveCfg = Debug|x64 | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Debug|x64.Build.0 = Debug|x64 | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Release|x64.ActiveCfg = Release|x64 | ||
{305177F7-5FAE-4BA3-BD41-40DB58C94C68}.Release|x64.Build.0 = Release|x64 | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {EF3A950A-3E00-4086-B25D-6BD890885B1D} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Application x:Class="CursorLocker.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:CursorLocker" | ||
StartupUri="MainWindow.xaml"> | ||
<Application.Resources> | ||
|
||
</Application.Resources> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Linq; | ||
using System.Windows; | ||
|
||
namespace CursorLocker | ||
{ | ||
public partial class App : Application | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{305177F7-5FAE-4BA3-BD41-40DB58C94C68}</ProjectGuid> | ||
<OutputType>WinExe</OutputType> | ||
<RootNamespace>CursorLocker</RootNamespace> | ||
<AssemblyName>CursorLocker</AssemblyName> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | ||
<WarningLevel>4</WarningLevel> | ||
<TargetFrameworkProfile /> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<Prefer32Bit>false</Prefer32Bit> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<OutputPath>bin\x64\Debug\</OutputPath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> | ||
<PlatformTarget>x64</PlatformTarget> | ||
<OutputPath>bin\x64\Release\</OutputPath> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.DirectoryServices.Protocols" /> | ||
<Reference Include="System.Drawing" /> | ||
<Reference Include="System.Windows.Forms" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xaml"> | ||
<RequiredTargetFramework>4.0</RequiredTargetFramework> | ||
</Reference> | ||
<Reference Include="UIAutomationClient" /> | ||
<Reference Include="UIAutomationTypes" /> | ||
<Reference Include="WindowsBase" /> | ||
<Reference Include="PresentationCore" /> | ||
<Reference Include="PresentationFramework" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ApplicationDefinition Include="App.xaml"> | ||
<Generator>MSBuild:Compile</Generator> | ||
<SubType>Designer</SubType> | ||
</ApplicationDefinition> | ||
<Page Include="MainWindow.xaml"> | ||
<Generator>MSBuild:Compile</Generator> | ||
<SubType>Designer</SubType> | ||
</Page> | ||
<Compile Include="App.xaml.cs"> | ||
<DependentUpon>App.xaml</DependentUpon> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="MainWindow.xaml.cs"> | ||
<DependentUpon>MainWindow.xaml</DependentUpon> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Properties\AssemblyInfo.cs"> | ||
<SubType>Code</SubType> | ||
</Compile> | ||
<Compile Include="Properties\Resources.Designer.cs"> | ||
<AutoGen>True</AutoGen> | ||
<DesignTime>True</DesignTime> | ||
<DependentUpon>Resources.resx</DependentUpon> | ||
</Compile> | ||
<Compile Include="Properties\Settings.Designer.cs"> | ||
<AutoGen>True</AutoGen> | ||
<DependentUpon>Settings.settings</DependentUpon> | ||
<DesignTimeSharedInput>True</DesignTimeSharedInput> | ||
</Compile> | ||
<EmbeddedResource Include="Properties\Resources.resx"> | ||
<Generator>ResXFileCodeGenerator</Generator> | ||
<LastGenOutput>Resources.Designer.cs</LastGenOutput> | ||
</EmbeddedResource> | ||
<None Include="app.config" /> | ||
<None Include="Properties\Settings.settings"> | ||
<Generator>SettingsSingleFileGenerator</Generator> | ||
<LastGenOutput>Settings.Designer.cs</LastGenOutput> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<WCFMetadata Include="Connected Services\" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Window | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" x:Name="CursorLocker" x:Class="CursorLocker.MainWindow" | ||
mc:Ignorable="d" | ||
Title="CursorLocker" Height="463" Width="537" Closing="Window_Closing" StateChanged="Window_StateChanged" KeyUp="Window_KeyUp"> | ||
<Grid> | ||
<Button x:Name="Browse" Content="Browse" HorizontalAlignment="Left" Margin="10,0,0,10" Width="95" Height="26" VerticalAlignment="Bottom" Click="Browse_Click" FontFamily="Tahoma" FontSize="14"/> | ||
<ListBox x:Name="PathBox" Margin="10,10,10,41" IsManipulationEnabled="True" IsTextSearchEnabled="False" SelectionMode="Extended" MouseUp="PathBox_MouseUp" KeyUp="PathBox_KeyUp" FontFamily="Tahoma" FontSize="14"/> | ||
<TextBox x:Name="PathText" Margin="110,0,10,10" TextWrapping="Wrap" Height="26" VerticalAlignment="Bottom" ToolTip="You can also enter just the executable name here. Press Enter to add it to the list." KeyUp="PathText_KeyUp" VerticalContentAlignment="Center" FontFamily="Tahoma" FontSize="14" MaxLines="1"/> | ||
</Grid> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.DirectoryServices.Protocols; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
using System.Windows.Media; | ||
using System.Windows.Automation; | ||
using NotifyIcon = System.Windows.Forms.NotifyIcon; | ||
using Screen = System.Windows.Forms.Screen; | ||
using OpenFileDialog = Microsoft.Win32.OpenFileDialog; | ||
|
||
namespace CursorLocker { | ||
public partial class MainWindow{ | ||
private readonly string _config; | ||
private readonly NotifyIcon _notifyIcon; | ||
private readonly SolidColorBrush _nameColor = new SolidColorBrush(Colors.DarkBlue); | ||
private readonly SolidColorBrush _errorColor = new SolidColorBrush(Colors.DarkRed); | ||
private readonly HashSet<string> _listeners = new HashSet<string>(); | ||
private readonly BackgroundWorker _bgWorker = new BackgroundWorker(); | ||
private readonly int _pid = Process.GetCurrentProcess().Id; | ||
|
||
public void OnFocusChangedHandler(object src, AutomationFocusChangedEventArgs args) { | ||
var element = src as AutomationElement; | ||
if (element == null) return; | ||
var curPid = element.Current.ProcessId; | ||
if (_pid == curPid) | ||
return; | ||
var names = new HashSet<string>{element.Current.Name}; | ||
var process = Process.GetProcessById(curPid); | ||
names.Add(process.ProcessName); | ||
names.Add(process.MainModule.ModuleName); | ||
names.Add(process.MainModule.FileName); | ||
Console.WriteLine($@" focus changed to {string.Join(", ", names)}"); | ||
bool overlaps; | ||
lock (_listeners) | ||
overlaps = _listeners.Overlaps(names); | ||
|
||
var bounded = System.Windows.Forms.Cursor.Clip == Screen.PrimaryScreen.Bounds; | ||
if (overlaps && !bounded) { | ||
lock (_listeners) | ||
names.IntersectWith(_listeners); | ||
|
||
System.Windows.Forms.Cursor.Clip = Screen.PrimaryScreen.Bounds; | ||
Console.WriteLine($@" locking cursor. triggered by: {string.Join("", names)}"); | ||
} | ||
else if (!overlaps && bounded){ | ||
System.Windows.Forms.Cursor.Clip = Rectangle.Empty; | ||
Console.WriteLine(@" unlocking cursor."); | ||
} | ||
} | ||
|
||
public MainWindow(){ | ||
InitializeComponent(); | ||
|
||
_bgWorker.DoWork += delegate{ | ||
Automation.RemoveAutomationFocusChangedEventHandler(OnFocusChangedHandler); | ||
Console.WriteLine(@" Focus handler disabled"); | ||
}; | ||
|
||
_notifyIcon = new NotifyIcon { | ||
Visible = false, | ||
Icon = SystemIcons.Application | ||
}; | ||
_notifyIcon.Click += (sender, args) => { | ||
Show(); | ||
WindowState = WindowState.Normal; | ||
_notifyIcon.Visible = false; | ||
}; | ||
|
||
_config = $"{Environment.ExpandEnvironmentVariables("%AppData%")}\\CursorLocker\\data.txt"; | ||
if (!File.Exists(_config)) | ||
return; | ||
using (var file = File.OpenText(_config)){ | ||
string line; | ||
while ((line = file.ReadLine()) != null) | ||
AddBoxItem(line); | ||
} | ||
} | ||
|
||
private void AddBoxItem(string item) { | ||
int c; | ||
lock (_listeners) { | ||
c = _listeners.Count; | ||
_listeners.Add(item); | ||
} | ||
if (c == 0) | ||
Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler); | ||
PathBox.Items.Add(new TextBlock { Text = item }); | ||
PathText.Text = ""; | ||
CheckPaths(); | ||
} | ||
|
||
private void CheckPaths() { | ||
foreach (var itemObj in PathBox.Items) { | ||
var item = (TextBlock)itemObj; | ||
if (!Path.IsPathRooted(item.Text)) | ||
item.Foreground = _nameColor; | ||
else if (!File.Exists(item.Text)) | ||
item.Foreground = _errorColor; | ||
} | ||
} | ||
|
||
private void Browse_Click(object sender, RoutedEventArgs e){ | ||
var dlg = new OpenFileDialog{ | ||
FileName = "Executable", | ||
DefaultExt = ".exe", | ||
Filter = "Application executables (.exe)|*.exe" | ||
}; | ||
|
||
if (dlg.ShowDialog() == true) | ||
AddBoxItem(dlg.FileName); | ||
} | ||
|
||
private void Window_Closing(object sender, CancelEventArgs e){ | ||
var path = Path.GetDirectoryName(_config); | ||
if (!Directory.Exists(path)) | ||
Directory.CreateDirectory(path ?? throw new DirectoryException($"Couldn't create persistance file '{_config}'.")); | ||
File.WriteAllLines(_config, PathBox.Items.Cast<TextBlock>().Select(tb=>tb.Text)); | ||
} | ||
|
||
private void Window_StateChanged(object sender, EventArgs e){ | ||
if (WindowState != WindowState.Minimized) return; | ||
Hide(); | ||
_notifyIcon.Visible = true; | ||
} | ||
|
||
private void Window_KeyUp(object sender, KeyEventArgs e) { | ||
if (e.Key == Key.F5) | ||
CheckPaths(); | ||
} | ||
|
||
private void PathBox_MouseUp(object sender, MouseButtonEventArgs e){ | ||
if(PathBox.SelectedIndex == -1) | ||
return; | ||
if (e.MouseDevice.DirectlyOver.GetType() == typeof(ScrollViewer)){ | ||
PathBox.UnselectAll(); | ||
PathText.Text = ""; | ||
return; | ||
} | ||
PathText.Text = ((TextBlock)PathBox.SelectedItem).Text; | ||
PathText.SelectionStart = PathText.Text.Length; | ||
} | ||
|
||
private void PathText_KeyUp(object sender, KeyEventArgs e) { | ||
if (e.Key != Key.Enter && e.Key != Key.Return) | ||
return; | ||
if(PathText.Text.Length == 0) | ||
return; | ||
lock (_listeners){ | ||
if (_listeners.Contains(PathText.Text)) | ||
return; | ||
} | ||
|
||
AddBoxItem(PathText.Text); | ||
} | ||
|
||
private void PathBox_KeyUp(object sender, KeyEventArgs e) { | ||
if(e.Key != Key.Delete && e.Key != Key.Back) | ||
return; | ||
var c = -1; | ||
foreach (var item in PathBox.SelectedItems.Cast<TextBlock>().ToList()){ | ||
PathBox.Items.Remove(item); | ||
lock (_listeners){ | ||
_listeners.Remove(item.Text); | ||
c = _listeners.Count; | ||
} | ||
} | ||
if (c == 0) | ||
_bgWorker.RunWorkerAsync(); | ||
PathText.Text = ""; | ||
} | ||
} | ||
} |
Oops, something went wrong.