Skip to content

Commit

Permalink
Fix auto-type
Browse files Browse the repository at this point in the history
  • Loading branch information
ihanson committed Jul 24, 2019
1 parent f801491 commit 4a5b7e6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
26 changes: 17 additions & 9 deletions RuleBuilder/Forms/ChangePassword.cs
@@ -1,5 +1,7 @@
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Windows.Input;
using KeePass.App;
using KeePassLib;
using KeePassLib.Security;

Expand All @@ -14,15 +16,19 @@ internal partial class ChangePassword : Form {
this.Generator = Rule.Serialization.Entry.EntryGenerator(entry);
this.txtOldPassword.Text = entry.Strings.Get(PwDefs.PasswordField).ReadString();
this.txtNewPassword.Text = this.Generator.NewPassword();
try {
this.OldPasswordHotKeyID = HotKey.RegisterHotKey(this, Keys.Z | Keys.Control | Keys.Shift);
} catch {
this.lblAutoTypeOld.Visible = false;
}
try {
this.NewPasswordHotKeyID = HotKey.RegisterHotKey(this, Keys.X | Keys.Control | Keys.Shift);
} catch {
this.lblAutoTypeNew.Visible = false;
if (this.Entry.GetAutoTypeEnabled() && AppPolicy.Try(AppPolicyId.AutoTypeWithoutContext)) {
try {
this.OldPasswordHotKeyID = HotKey.RegisterHotKey(this, Keys.Z | Keys.Control | Keys.Shift);
this.lblAutoTypeOld.Visible = true;
} catch (HotKeyException e) {
_ = e;
}
try {
this.NewPasswordHotKeyID = HotKey.RegisterHotKey(this, Keys.X | Keys.Control | Keys.Shift);
this.lblAutoTypeNew.Visible = true;
} catch (HotKeyException e) {
_ = e;
}
}
}
public Rule.IPasswordGenerator Generator { get; private set; }
Expand All @@ -39,6 +45,7 @@ internal partial class ChangePassword : Form {
}
protected override void WndProc(ref Message m) {
if (m.Msg == HotKeyMessage) {
while (!ControlKeysUp()) { }
int hotKeyID = (int)m.WParam;
if (hotKeyID == this.OldPasswordHotKeyID) {
_ = KeePass.Util.AutoType.PerformIntoCurrentWindow(this.Entry, this.Database, EscapeAutoType(this.txtOldPassword.Text));
Expand All @@ -49,6 +56,7 @@ internal partial class ChangePassword : Form {
base.WndProc(ref m);
}
private static string EscapeAutoType(string text) => Regex.Replace(text, @"[+%^~()[\]{}]", (Match match) => $"{{{match.Value}}}");
private static bool ControlKeysUp() => Keyboard.IsKeyUp(Key.LeftCtrl) && Keyboard.IsKeyUp(Key.RightCtrl) && Keyboard.IsKeyUp(Key.LeftShift) && Keyboard.IsKeyUp(Key.RightShift);
private void SaveNewPassword(object sender, System.EventArgs e) {
string oldPassword = this.Entry.Strings.Get(PwDefs.PasswordField).ReadString();
string newPassword = this.txtNewPassword.Text;
Expand Down
6 changes: 6 additions & 0 deletions RuleBuilder/Forms/ChangePassword.resx
Expand Up @@ -171,6 +171,9 @@
<data name="lblAutoTypeOld.Text" xml:space="preserve">
<value>Auto-type: Ctrl+Shift+Z</value>
</data>
<data name="lblAutoTypeOld.Visible" type="System.Boolean, mscorlib">
<value>False</value>
</data>
<data name="&gt;&gt;lblAutoTypeOld.Name" xml:space="preserve">
<value>lblAutoTypeOld</value>
</data>
Expand Down Expand Up @@ -201,6 +204,9 @@
<data name="lblAutoTypeNew.Text" xml:space="preserve">
<value>Auto-type: Ctrl+Shift+X</value>
</data>
<data name="lblAutoTypeNew.Visible" type="System.Boolean, mscorlib">
<value>False</value>
</data>
<data name="&gt;&gt;lblAutoTypeNew.Name" xml:space="preserve">
<value>lblAutoTypeNew</value>
</data>
Expand Down
4 changes: 3 additions & 1 deletion RuleBuilder/RuleBuilder.csproj
Expand Up @@ -31,15 +31,17 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="KeePass, Version=2.41.0.19744, Culture=neutral, PublicKeyToken=fed2ed7716aecf5c, processorArchitecture=MSIL">
<Reference Include="KeePass, Version=2.42.1.36739, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\KeePass\KeePass.exe</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Forms\ChangePassword.cs">
Expand Down

0 comments on commit 4a5b7e6

Please sign in to comment.