diff --git a/KeyGo.sln b/KeyGo.sln new file mode 100644 index 0000000..ddfbf34 --- /dev/null +++ b/KeyGo.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31205.134 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyGo", "KeyGo\KeyGo.csproj", "{09416937-868E-4EE4-80EE-5E6FA81E2811}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {09416937-868E-4EE4-80EE-5E6FA81E2811}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {09416937-868E-4EE4-80EE-5E6FA81E2811}.Debug|Any CPU.Build.0 = Debug|Any CPU + {09416937-868E-4EE4-80EE-5E6FA81E2811}.Release|Any CPU.ActiveCfg = Release|Any CPU + {09416937-868E-4EE4-80EE-5E6FA81E2811}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E8C7BFA9-5EDE-4598-B94E-B2184ACE4D99} + EndGlobalSection +EndGlobal diff --git a/KeyGo/App.config b/KeyGo/App.config new file mode 100644 index 0000000..88fa402 --- /dev/null +++ b/KeyGo/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/KeyGo/AppHotKey.cs b/KeyGo/AppHotKey.cs new file mode 100644 index 0000000..a5abd64 --- /dev/null +++ b/KeyGo/AppHotKey.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace KeyGo +{ + public class AppHotKey + { + [DllImport("kernel32.dll")] + public static extern uint GetLastError(); + //如果函数执行成功,返回值不为0。 + //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。 + [DllImport("user32.dll", SetLastError = true)] + public static extern bool RegisterHotKey( + IntPtr hWnd, //要定义热键的窗口的句柄 + int id, //定义热键ID(不能与其它ID重复) + KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 + Keys vk //定义热键的内容 + ); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool UnregisterHotKey( + IntPtr hWnd, //要取消热键的窗口的句柄 + int id //要取消热键的ID + ); + + //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值) + [Flags] + public enum KeyModifiers + { + None = 0, + Alt = 1, + Ctrl = 2, + Shift = 4, + WindowsKey = 8 + } + /// + /// 注册热键 + /// + /// 窗口句柄 + /// 热键ID + /// 组合键 + /// 热键 + public static void RegKey(IntPtr hwnd, int hotKey_id, KeyModifiers keyModifiers, Keys key) + { + //try + //{ + if (!RegisterHotKey(hwnd, hotKey_id, keyModifiers, key)) + { + throw new Win32Exception(); + //int code = Marshal.GetLastWin32Error(); + //if (code == 1409) + // MessageBox.Show("热键被占用!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + //else + // MessageBox.Show("注册热键失败!错误代码:" + code.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + //} + //catch (Exception ex) + //{ + // MessageBox.Show("注册热键失败!异常信息:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); + //} + } + /// + /// 注销热键 + /// + /// 窗口句柄 + /// 热键ID + public static void UnRegKey(IntPtr hwnd, int hotKey_id) + { + //注销Id号为hotKey_id的热键设定 + UnregisterHotKey(hwnd, hotKey_id); + } + } +} diff --git a/KeyGo/FormMain.Designer.cs b/KeyGo/FormMain.Designer.cs new file mode 100644 index 0000000..0058ed3 --- /dev/null +++ b/KeyGo/FormMain.Designer.cs @@ -0,0 +1,69 @@ + +namespace KeyGo +{ + partial class FormMain + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.TxtHotKey1 = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // TxtHotKey1 + // + this.TxtHotKey1.BackColor = System.Drawing.Color.White; + this.TxtHotKey1.Location = new System.Drawing.Point(96, 122); + this.TxtHotKey1.Name = "TxtHotKey1"; + this.TxtHotKey1.ReadOnly = true; + this.TxtHotKey1.Size = new System.Drawing.Size(213, 23); + this.TxtHotKey1.TabIndex = 0; + this.TxtHotKey1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + this.TxtHotKey1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtHotKey1_KeyDown); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(482, 315); + this.Controls.Add(this.TxtHotKey1); + this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.Name = "FormMain"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "KeyGo!"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FormMain_FormClosed); + this.Load += new System.EventHandler(this.FormMain_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox TxtHotKey1; + } +} + diff --git a/KeyGo/FormMain.cs b/KeyGo/FormMain.cs new file mode 100644 index 0000000..f2acb4c --- /dev/null +++ b/KeyGo/FormMain.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace KeyGo +{ + public partial class FormMain : Form + { + public FormMain() + { + InitializeComponent(); + } + + + + void ProcessHotkey(int hotKey_id) + { + //if (hotKey_id == 100) + // MessageBox.Show("Test"); + } + + + + + + private void TxtHotKey1_KeyDown(object sender, KeyEventArgs e) + { + TxtHotKey1.Text = e.Modifiers.ToString(); + Console.WriteLine($"down\t{e.KeyData}"); + } + + + + + + + + + + + private void FormMain_Load(object sender, EventArgs e) + { + //AppHotKey.RegKey(Handle, 100, AppHotKey.KeyModifiers.Ctrl, Keys.W); + } + + private void FormMain_FormClosed(object sender, FormClosedEventArgs e) + { + //AppHotKey.UnRegKey(Handle, 100); + } + + + + #region 窗体消息 热键回调 + private const int WM_HOTKEY = 0x312; + protected override void WndProc(ref Message m) + { + base.WndProc(ref m); + switch (m.Msg) + { + case WM_HOTKEY: + ProcessHotkey(m.WParam.ToInt32()); + break; + } + } + #endregion + } +} \ No newline at end of file diff --git a/KeyGo/FormMain.resx b/KeyGo/FormMain.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/KeyGo/FormMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/KeyGo/KeyGo.csproj b/KeyGo/KeyGo.csproj new file mode 100644 index 0000000..affed52 --- /dev/null +++ b/KeyGo/KeyGo.csproj @@ -0,0 +1,84 @@ + + + + + Debug + AnyCPU + {09416937-868E-4EE4-80EE-5E6FA81E2811} + WinExe + KeyGo + KeyGo + v4.5.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + Form + + + FormMain.cs + + + + + FormMain.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git a/KeyGo/Program.cs b/KeyGo/Program.cs new file mode 100644 index 0000000..1ee0e4b --- /dev/null +++ b/KeyGo/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace KeyGo +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FormMain()); + } + } +} diff --git a/KeyGo/Properties/AssemblyInfo.cs b/KeyGo/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..8316384 --- /dev/null +++ b/KeyGo/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("KeyGo")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("KeyGo")] +[assembly: AssemblyCopyright("Copyright © 2021")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("09416937-868e-4ee4-80ee-5e6fa81e2811")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/KeyGo/Properties/Resources.Designer.cs b/KeyGo/Properties/Resources.Designer.cs new file mode 100644 index 0000000..66926cc --- /dev/null +++ b/KeyGo/Properties/Resources.Designer.cs @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + + +namespace KeyGo.Properties +{ + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("KeyGo.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/KeyGo/Properties/Resources.resx b/KeyGo/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/KeyGo/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/KeyGo/Properties/Settings.Designer.cs b/KeyGo/Properties/Settings.Designer.cs new file mode 100644 index 0000000..702b2e7 --- /dev/null +++ b/KeyGo/Properties/Settings.Designer.cs @@ -0,0 +1,29 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +namespace KeyGo.Properties +{ + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/KeyGo/Properties/Settings.settings b/KeyGo/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/KeyGo/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + +