mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
Add replace win + r option
This commit is contained in:
343
WinAlfred/Helper/GlobalKeyboardHook.cs
Normal file
343
WinAlfred/Helper/GlobalKeyboardHook.cs
Normal file
@@ -0,0 +1,343 @@
|
||||
/// KEYBOARD.CS
|
||||
/// (c) 2006 by Emma Burrows
|
||||
/// This file contains the following items:
|
||||
/// - KeyboardHook: class to enable low-level keyboard hook using
|
||||
/// the Windows API.
|
||||
/// - KeyboardHookEventHandler: delegate to handle the KeyIntercepted
|
||||
/// event raised by the KeyboardHook class.
|
||||
/// - KeyboardHookEventArgs: EventArgs class to contain the information
|
||||
/// returned by the KeyIntercepted event.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace WinAlfred.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// Low-level keyboard intercept class to trap and suppress system keys.
|
||||
/// </summary>
|
||||
public class GlobalKeyboardHook : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Parameters accepted by the KeyboardHook constructor.
|
||||
/// </summary>
|
||||
public enum Parameters
|
||||
{
|
||||
None,
|
||||
AllowAltTab,
|
||||
AllowWindowsKey,
|
||||
AllowAltTabAndWindows,
|
||||
PassAllKeysToNextApp
|
||||
}
|
||||
|
||||
//Internal parameters
|
||||
private bool PassAllKeysToNextApp = false;
|
||||
private bool AllowAltTab = false;
|
||||
private bool AllowWindowsKey = false;
|
||||
|
||||
//Keyboard API constants
|
||||
private const int WH_KEYBOARD_LL = 13;
|
||||
private const int WM_KEYUP = 0x0101;
|
||||
private const int WM_SYSKEYUP = 0x0105;
|
||||
|
||||
//Modifier key constants
|
||||
private const int VK_SHIFT = 0x10;
|
||||
private const int VK_CONTROL = 0x11;
|
||||
private const int VK_MENU = 0x12;
|
||||
private const int VK_CAPITAL = 0x14;
|
||||
|
||||
//Variables used in the call to SetWindowsHookEx
|
||||
private HookHandlerDelegate proc;
|
||||
private IntPtr hookID = IntPtr.Zero;
|
||||
internal delegate IntPtr HookHandlerDelegate(
|
||||
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
|
||||
|
||||
/// <summary>
|
||||
/// Event triggered when a keystroke is intercepted by the
|
||||
/// low-level hook.
|
||||
/// </summary>
|
||||
public event KeyboardHookEventHandler KeyIntercepted;
|
||||
|
||||
// Structure returned by the hook whenever a key is pressed
|
||||
internal struct KBDLLHOOKSTRUCT
|
||||
{
|
||||
public int vkCode;
|
||||
int scanCode;
|
||||
public int flags;
|
||||
int time;
|
||||
int dwExtraInfo;
|
||||
}
|
||||
|
||||
#region Constructors
|
||||
/// <summary>
|
||||
/// Sets up a keyboard hook to trap all keystrokes without
|
||||
/// passing any to other applications.
|
||||
/// </summary>
|
||||
public GlobalKeyboardHook()
|
||||
{
|
||||
proc = new HookHandlerDelegate(HookCallback);
|
||||
using (Process curProcess = Process.GetCurrentProcess())
|
||||
using (ProcessModule curModule = curProcess.MainModule)
|
||||
{
|
||||
hookID = NativeMethods.SetWindowsHookEx(WH_KEYBOARD_LL, proc,
|
||||
NativeMethods.GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up a keyboard hook with custom parameters.
|
||||
/// </summary>
|
||||
/// <param name="param">A valid name from the Parameter enum; otherwise, the
|
||||
/// default parameter Parameter.None will be used.</param>
|
||||
public GlobalKeyboardHook(string param)
|
||||
: this()
|
||||
{
|
||||
if (!String.IsNullOrEmpty(param) && Enum.IsDefined(typeof(Parameters), param))
|
||||
{
|
||||
SetParameters((Parameters)Enum.Parse(typeof(Parameters), param));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets up a keyboard hook with custom parameters.
|
||||
/// </summary>
|
||||
/// <param name="param">A value from the Parameters enum.</param>
|
||||
public GlobalKeyboardHook(Parameters param)
|
||||
: this()
|
||||
{
|
||||
SetParameters(param);
|
||||
}
|
||||
|
||||
private void SetParameters(Parameters param)
|
||||
{
|
||||
switch (param)
|
||||
{
|
||||
case Parameters.None:
|
||||
break;
|
||||
case Parameters.AllowAltTab:
|
||||
AllowAltTab = true;
|
||||
break;
|
||||
case Parameters.AllowWindowsKey:
|
||||
AllowWindowsKey = true;
|
||||
break;
|
||||
case Parameters.AllowAltTabAndWindows:
|
||||
AllowAltTab = true;
|
||||
AllowWindowsKey = true;
|
||||
break;
|
||||
case Parameters.PassAllKeysToNextApp:
|
||||
PassAllKeysToNextApp = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Check Modifier keys
|
||||
/// <summary>
|
||||
/// Checks whether Alt, Shift, Control or CapsLock
|
||||
/// is enabled at the same time as another key.
|
||||
/// Modify the relevant sections and return type
|
||||
/// depending on what you want to do with modifier keys.
|
||||
/// </summary>
|
||||
private void CheckModifiers()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if ((NativeMethods.GetKeyState(VK_CAPITAL) & 0x0001) != 0)
|
||||
{
|
||||
//CAPSLOCK is ON
|
||||
sb.AppendLine("Capslock is enabled.");
|
||||
}
|
||||
|
||||
if ((NativeMethods.GetKeyState(VK_SHIFT) & 0x8000) != 0)
|
||||
{
|
||||
//SHIFT is pressed
|
||||
sb.AppendLine("Shift is pressed.");
|
||||
}
|
||||
if ((NativeMethods.GetKeyState(VK_CONTROL) & 0x8000) != 0)
|
||||
{
|
||||
//CONTROL is pressed
|
||||
sb.AppendLine("Control is pressed.");
|
||||
}
|
||||
if ((NativeMethods.GetKeyState(VK_MENU) & 0x8000) != 0)
|
||||
{
|
||||
//ALT is pressed
|
||||
sb.AppendLine("Alt is pressed.");
|
||||
}
|
||||
Console.WriteLine(sb.ToString());
|
||||
}
|
||||
#endregion Check Modifier keys
|
||||
|
||||
#region Hook Callback Method
|
||||
/// <summary>
|
||||
/// Processes the key event captured by the hook.
|
||||
/// </summary>
|
||||
private IntPtr HookCallback(
|
||||
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
|
||||
{
|
||||
bool AllowKey = PassAllKeysToNextApp;
|
||||
|
||||
//Filter wParam for KeyUp events only
|
||||
if (nCode >= 0)
|
||||
{
|
||||
if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)
|
||||
{
|
||||
|
||||
// Check for modifier keys, but only if the key being
|
||||
// currently processed isn't a modifier key (in other
|
||||
// words, CheckModifiers will only run if Ctrl, Shift,
|
||||
// CapsLock or Alt are active at the same time as
|
||||
// another key)
|
||||
if (!(lParam.vkCode >= 160 && lParam.vkCode <= 164))
|
||||
{
|
||||
CheckModifiers();
|
||||
}
|
||||
|
||||
// Check for key combinations that are allowed to
|
||||
// get through to Windows
|
||||
//
|
||||
// Ctrl+Esc or Windows key
|
||||
if (AllowWindowsKey)
|
||||
{
|
||||
switch (lParam.flags)
|
||||
{
|
||||
//Ctrl+Esc
|
||||
case 0:
|
||||
if (lParam.vkCode == 27)
|
||||
AllowKey = true;
|
||||
break;
|
||||
|
||||
//Windows keys
|
||||
case 1:
|
||||
if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
|
||||
AllowKey = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Alt+Tab
|
||||
if (AllowAltTab)
|
||||
{
|
||||
if ((lParam.flags == 32) && (lParam.vkCode == 9))
|
||||
AllowKey = true;
|
||||
}
|
||||
|
||||
OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, AllowKey));
|
||||
}
|
||||
|
||||
//If this key is being suppressed, return a dummy value
|
||||
if (AllowKey == false)
|
||||
return (System.IntPtr)1;
|
||||
}
|
||||
//Pass key to next application
|
||||
return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Event Handling
|
||||
/// <summary>
|
||||
/// Raises the KeyIntercepted event.
|
||||
/// </summary>
|
||||
/// <param name="e">An instance of KeyboardHookEventArgs</param>
|
||||
public void OnKeyIntercepted(KeyboardHookEventArgs e)
|
||||
{
|
||||
if (KeyIntercepted != null)
|
||||
KeyIntercepted(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delegate for KeyboardHook event handling.
|
||||
/// </summary>
|
||||
/// <param name="e">An instance of InterceptKeysEventArgs.</param>
|
||||
public delegate void KeyboardHookEventHandler(KeyboardHookEventArgs e);
|
||||
|
||||
/// <summary>
|
||||
/// Event arguments for the KeyboardHook class's KeyIntercepted event.
|
||||
/// </summary>
|
||||
public class KeyboardHookEventArgs : System.EventArgs
|
||||
{
|
||||
|
||||
private string keyName;
|
||||
private int keyCode;
|
||||
private bool passThrough;
|
||||
|
||||
/// <summary>
|
||||
/// The name of the key that was pressed.
|
||||
/// </summary>
|
||||
public string KeyName
|
||||
{
|
||||
get { return keyName; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The virtual key code of the key that was pressed.
|
||||
/// </summary>
|
||||
public int KeyCode
|
||||
{
|
||||
get { return keyCode; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True if this key combination was passed to other applications,
|
||||
/// false if it was trapped.
|
||||
/// </summary>
|
||||
public bool PassThrough
|
||||
{
|
||||
get { return passThrough; }
|
||||
}
|
||||
|
||||
public KeyboardHookEventArgs(int evtKeyCode, bool evtPassThrough)
|
||||
{
|
||||
keyName = ((Keys)evtKeyCode).ToString();
|
||||
keyCode = evtKeyCode;
|
||||
passThrough = evtPassThrough;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDisposable Members
|
||||
/// <summary>
|
||||
/// Releases the keyboard hook.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
NativeMethods.UnhookWindowsHookEx(hookID);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Native methods
|
||||
|
||||
[ComVisible(false),
|
||||
System.Security.SuppressUnmanagedCodeSecurity()]
|
||||
internal class NativeMethods
|
||||
{
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr SetWindowsHookEx(int idHook,
|
||||
HookHandlerDelegate lpfn, IntPtr hMod, uint dwThreadId);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
|
||||
IntPtr wParam, ref KBDLLHOOKSTRUCT lParam);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
|
||||
public static extern short GetKeyState(int keyCode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace WinAlfred.Helper
|
||||
IniParser parser = new IniParser("config.ini");
|
||||
|
||||
public string Theme { get; set; }
|
||||
public bool ReplaceWinR { get; set; }
|
||||
|
||||
private Settings()
|
||||
{
|
||||
@@ -23,11 +24,19 @@ namespace WinAlfred.Helper
|
||||
{
|
||||
if (!File.Exists(configPath)) File.Create(configPath);
|
||||
Theme = parser.GetSetting("ui", "theme");
|
||||
|
||||
string replaceWinRStr = parser.GetSetting("hotkey", "replaceWinR");
|
||||
bool replace = true;
|
||||
if (bool.TryParse(replaceWinRStr, out replace))
|
||||
{
|
||||
ReplaceWinR = replace;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSettings()
|
||||
{
|
||||
parser.AddSetting("ui", "theme", Theme);
|
||||
parser.AddSetting("hotkey", "replaceWinR", ReplaceWinR.ToString());
|
||||
parser.SaveSettings();
|
||||
}
|
||||
|
||||
|
||||
245
WinAlfred/KeyboardListener.cs
Normal file
245
WinAlfred/KeyboardListener.cs
Normal file
@@ -0,0 +1,245 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace WinAlfred
|
||||
{
|
||||
public enum KeyEvent : int
|
||||
{
|
||||
/// <summary>
|
||||
/// Key down
|
||||
/// </summary>
|
||||
WM_KEYDOWN = 256,
|
||||
|
||||
/// <summary>
|
||||
/// Key up
|
||||
/// </summary>
|
||||
WM_KEYUP = 257,
|
||||
|
||||
/// <summary>
|
||||
/// System key up
|
||||
/// </summary>
|
||||
WM_SYSKEYUP = 261,
|
||||
|
||||
/// <summary>
|
||||
/// System key down
|
||||
/// </summary>
|
||||
WM_SYSKEYDOWN = 260
|
||||
}
|
||||
|
||||
public class SpecialKeyState
|
||||
{
|
||||
public bool CtrlPressed { get; set; }
|
||||
public bool ShiftPressed { get; set; }
|
||||
public bool AltPressed { get; set; }
|
||||
public bool WinPressed { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Listens keyboard globally.
|
||||
///
|
||||
/// <remarks>Uses WH_KEYBOARD_LL.</remarks>
|
||||
/// </summary>
|
||||
public class KeyboardListener : IDisposable
|
||||
{
|
||||
private InterceptKeys.LowLevelKeyboardProc hookedLowLevelKeyboardProc;
|
||||
private IntPtr hookId = IntPtr.Zero;
|
||||
public delegate bool KeyboardCallback(KeyEvent keyEvent, int vkCode, SpecialKeyState state);
|
||||
public event KeyboardCallback hookedKeyboardCallback;
|
||||
|
||||
//Modifier key constants
|
||||
private const int VK_SHIFT = 0x10;
|
||||
private const int VK_CONTROL = 0x11;
|
||||
private const int VK_ALT = 0x12;
|
||||
private const int VK_WIN = 91;
|
||||
|
||||
public KeyboardListener()
|
||||
{
|
||||
// We have to store the LowLevelKeyboardProc, so that it is not garbage collected runtime
|
||||
hookedLowLevelKeyboardProc = LowLevelKeyboardProc;
|
||||
// Set the hook
|
||||
hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc);
|
||||
}
|
||||
|
||||
private SpecialKeyState CheckModifiers()
|
||||
{
|
||||
SpecialKeyState state = new SpecialKeyState();
|
||||
if ((InterceptKeys.GetKeyState(VK_SHIFT) & 0x8000) != 0)
|
||||
{
|
||||
//SHIFT is pressed
|
||||
state.ShiftPressed = true;
|
||||
}
|
||||
if ((InterceptKeys.GetKeyState(VK_CONTROL) & 0x8000) != 0)
|
||||
{
|
||||
//CONTROL is pressed
|
||||
state.CtrlPressed = true;
|
||||
}
|
||||
if ((InterceptKeys.GetKeyState(VK_ALT) & 0x8000) != 0)
|
||||
{
|
||||
//ALT is pressed
|
||||
state.AltPressed = true;
|
||||
}
|
||||
if ((InterceptKeys.GetKeyState(VK_WIN) & 0x8000) != 0)
|
||||
{
|
||||
//ALT is pressed
|
||||
state.WinPressed = true;
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
bool continues = true;
|
||||
|
||||
if (nCode >= 0)
|
||||
{
|
||||
if (wParam.ToUInt32() == (int)KeyEvent.WM_KEYDOWN ||
|
||||
wParam.ToUInt32() == (int)KeyEvent.WM_KEYUP ||
|
||||
wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYDOWN ||
|
||||
wParam.ToUInt32() == (int)KeyEvent.WM_SYSKEYUP)
|
||||
{
|
||||
continues = hookedKeyboardCallback((KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), CheckModifiers());
|
||||
}
|
||||
}
|
||||
|
||||
if (continues)
|
||||
{
|
||||
return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam);
|
||||
}
|
||||
return (IntPtr)1;
|
||||
}
|
||||
|
||||
~KeyboardListener()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
InterceptKeys.UnhookWindowsHookEx(hookId);
|
||||
}
|
||||
}
|
||||
|
||||
public static class InterceptKeys
|
||||
{
|
||||
public delegate IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam);
|
||||
|
||||
private static int WH_KEYBOARD_LL = 13;
|
||||
|
||||
public static IntPtr SetHook(LowLevelKeyboardProc proc)
|
||||
{
|
||||
using (Process curProcess = Process.GetCurrentProcess())
|
||||
using (ProcessModule curModule = curProcess.MainModule)
|
||||
{
|
||||
return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
|
||||
}
|
||||
}
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, UIntPtr wParam, IntPtr lParam);
|
||||
|
||||
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
||||
public static extern IntPtr GetModuleHandle(string lpModuleName);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
|
||||
public static extern short GetKeyState(int keyCode);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
|
||||
|
||||
|
||||
public static void SendKeyStroke(int funckey, int key)
|
||||
{
|
||||
INPUT[] input = new INPUT[4];
|
||||
input[0].type = input[1].type = input[2].type = input[3].type = (int)InputType.INPUT_KEYBOARD;
|
||||
input[0].ki.wVk = input[2].ki.wVk = (short) funckey;
|
||||
input[1].ki.wVk = input[3].ki.wVk = (short) key;
|
||||
|
||||
input[2].ki.dwFlags = input[3].ki.dwFlags =(int) KEYEVENTF.KEYUP;
|
||||
|
||||
SendInput((uint)input.Length, input, Marshal.SizeOf(input[0]));
|
||||
}
|
||||
}
|
||||
|
||||
public enum InputType
|
||||
{
|
||||
INPUT_MOUSE = 0,
|
||||
INPUT_KEYBOARD = 1,
|
||||
INPUT_HARDWARE = 2,
|
||||
}
|
||||
[Flags()]
|
||||
public enum MOUSEEVENTF
|
||||
{
|
||||
MOVE = 0x0001, //mouse move
|
||||
LEFTDOWN = 0x0002, //left button down
|
||||
LEFTUP = 0x0004, //left button up
|
||||
RIGHTDOWN = 0x0008, //right button down
|
||||
RIGHTUP = 0x0010, //right button up
|
||||
MIDDLEDOWN = 0x0020, //middle button down
|
||||
MIDDLEUP = 0x0040, //middle button up
|
||||
XDOWN = 0x0080, //x button down
|
||||
XUP = 0x0100, //x button down
|
||||
WHEEL = 0x0800, //wheel button rolled
|
||||
VIRTUALDESK = 0x4000, //map to entire virtual desktop
|
||||
ABSOLUTE = 0x8000, //absolute move
|
||||
}
|
||||
|
||||
[Flags()]
|
||||
public enum KEYEVENTF
|
||||
{
|
||||
EXTENDEDKEY = 0x0001,
|
||||
KEYUP = 0x0002,
|
||||
UNICODE = 0x0004,
|
||||
SCANCODE = 0x0008,
|
||||
}
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct INPUT
|
||||
{
|
||||
[FieldOffset(0)]
|
||||
public Int32 type;//0-MOUSEINPUT;1-KEYBDINPUT;2-HARDWAREINPUT
|
||||
[FieldOffset(4)]
|
||||
public KEYBDINPUT ki;
|
||||
[FieldOffset(4)]
|
||||
public MOUSEINPUT mi;
|
||||
[FieldOffset(4)]
|
||||
public HARDWAREINPUT hi;
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct MOUSEINPUT
|
||||
{
|
||||
public Int32 dx;
|
||||
public Int32 dy;
|
||||
public Int32 mouseData;
|
||||
public Int32 dwFlags;
|
||||
public Int32 time;
|
||||
public IntPtr dwExtraInfo;
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct KEYBDINPUT
|
||||
{
|
||||
public Int16 wVk;
|
||||
public Int16 wScan;
|
||||
public Int32 dwFlags;
|
||||
public Int32 time;
|
||||
public IntPtr dwExtraInfo;
|
||||
}
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct HARDWAREINPUT
|
||||
{
|
||||
public Int32 uMsg;
|
||||
public Int16 wParamL;
|
||||
public Int16 wParamH;
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,12 @@
|
||||
SizeToContent="Height"
|
||||
ResizeMode="NoResize"
|
||||
WindowStyle="None"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
WindowStartupLocation="Manual"
|
||||
ShowInTaskbar="False"
|
||||
Style="{DynamicResource WindowStyle}"
|
||||
Icon="Images\app.png"
|
||||
>
|
||||
<Grid Style="{DynamicResource GridStyle}">
|
||||
<Grid Style="{DynamicResource GridStyle}" x:Name="grid">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition ></RowDefinition>
|
||||
<RowDefinition></RowDefinition>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
@@ -26,6 +28,9 @@ namespace WinAlfred
|
||||
private bool queryHasReturn = false;
|
||||
SelectedRecords selectedRecords = new SelectedRecords();
|
||||
|
||||
private KeyboardListener keyboardListener = new KeyboardListener();
|
||||
private bool WinRStroked = false;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -36,7 +41,9 @@ namespace WinAlfred
|
||||
ThreadPool.SetMaxThreads(30, 10);
|
||||
InitProgressbarAnimation();
|
||||
|
||||
|
||||
ChangeStyles(Settings.Instance.Theme);
|
||||
|
||||
}
|
||||
|
||||
private void WakeupApp()
|
||||
@@ -45,7 +52,7 @@ namespace WinAlfred
|
||||
//This is caused by the Virtual Mermory Page Mechanisam. So, our solution is execute some codes in every min
|
||||
//which may prevent sysetem uninstall memory from RAM to disk.
|
||||
|
||||
System.Timers.Timer t = new System.Timers.Timer(1000 * 60 * 3) { AutoReset = true, Enabled = true };
|
||||
System.Timers.Timer t = new System.Timers.Timer(1000 * 60 * 5) { AutoReset = true, Enabled = true };
|
||||
t.Elapsed += (o, e) => Dispatcher.Invoke(new Action(() =>
|
||||
{
|
||||
if (Visibility != Visibility.Visible)
|
||||
@@ -87,8 +94,7 @@ namespace WinAlfred
|
||||
|
||||
private void resultCtrl_resultItemChangedEvent()
|
||||
{
|
||||
//Height = resultCtrl.pnlContainer.ActualHeight + tbQuery.Height + tbQuery.Margin.Top + tbQuery.Margin.Bottom;
|
||||
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Top = 10 } : new Thickness { Top = 0 };
|
||||
resultCtrl.Margin = resultCtrl.GetCurrentResultCount() > 0 ? new Thickness { Top = grid.Margin.Top } : new Thickness { Top = 0 };
|
||||
}
|
||||
|
||||
private void OnHotKey(object sender, KeyPressedEventArgs e)
|
||||
@@ -153,8 +159,9 @@ namespace WinAlfred
|
||||
{
|
||||
Show();
|
||||
Activate();
|
||||
tbQuery.Focus();
|
||||
tbQuery.SelectAll();
|
||||
Focus();
|
||||
tbQuery.Focus();
|
||||
}
|
||||
|
||||
public void ParseArgs(string[] args)
|
||||
@@ -200,6 +207,9 @@ namespace WinAlfred
|
||||
|
||||
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Left = (SystemParameters.PrimaryScreenWidth - ActualWidth) / 2;
|
||||
Top = (SystemParameters.PrimaryScreenHeight - ActualHeight) / 3;
|
||||
|
||||
Plugins.Init(this);
|
||||
cmdDispatcher = new Command(this);
|
||||
InitialTray();
|
||||
@@ -208,6 +218,32 @@ namespace WinAlfred
|
||||
WakeupApp();
|
||||
//var engine = new Jurassic.ScriptEngine();
|
||||
//MessageBox.Show(engine.Evaluate("5 * 10 + 2").ToString());
|
||||
keyboardListener.hookedKeyboardCallback += KListener_hookedKeyboardCallback;
|
||||
}
|
||||
|
||||
private bool KListener_hookedKeyboardCallback(KeyEvent keyevent, int vkcode, SpecialKeyState state)
|
||||
{
|
||||
if (Settings.Instance.ReplaceWinR)
|
||||
{
|
||||
if (keyevent == KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
resultCtrl.Clear();
|
||||
ShowWinAlfred();
|
||||
ChangeQuery(">");
|
||||
WinRStroked = true;
|
||||
}));
|
||||
return false;
|
||||
}
|
||||
if (keyevent == KeyEvent.WM_KEYUP && WinRStroked && vkcode == (int)Keys.LWin)
|
||||
{
|
||||
WinRStroked = false;
|
||||
WindowsInput.InputSimulator.SimulateModifiedKeyStroke(WindowsInput.VirtualKeyCode.LWIN, WindowsInput.VirtualKeyCode.CONTROL);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void TbQuery_OnPreviewKeyDown(object sender, KeyEventArgs e)
|
||||
@@ -250,7 +286,7 @@ namespace WinAlfred
|
||||
progressBar.Dispatcher.Invoke(new Action(StopProgress));
|
||||
if (list.Count > 0)
|
||||
{
|
||||
//todo:this used be opened to users, it's they choise use it or not in thier workflows
|
||||
//todo:this should be opened to users, it's their choise to use it or not in thier workflows
|
||||
list.ForEach(o =>
|
||||
{
|
||||
if (o.AutoAjustScore) o.Score += selectedRecords.GetSelectedCount(o);
|
||||
@@ -314,6 +350,5 @@ namespace WinAlfred
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,15 @@
|
||||
ResizeMode="NoResize"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Height="407.447" Width="843.989">
|
||||
<Grid>
|
||||
<TextBlock Text="Theme:"/>
|
||||
<ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" Margin="59.83,0,0,0" VerticalAlignment="Top" Width="120"/>
|
||||
</Grid>
|
||||
<StackPanel Orientation="Vertical" Margin="10">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="Theme:" />
|
||||
<ComboBox x:Name="themeComboBox" SelectionChanged="ThemeComboBox_OnSelectionChanged" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
|
||||
</StackPanel>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<CheckBox x:Name="cbReplaceWinR" />
|
||||
<TextBlock Text="Replace Win+R:" />
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
</Window>
|
||||
|
||||
@@ -16,6 +16,16 @@ namespace WinAlfred
|
||||
this.mainWindow = mainWindow;
|
||||
InitializeComponent();
|
||||
Loaded += Setting_Loaded;
|
||||
cbReplaceWinR.Checked += (o, e) =>
|
||||
{
|
||||
Settings.Instance.ReplaceWinR = true;
|
||||
Settings.Instance.SaveSettings();
|
||||
};
|
||||
cbReplaceWinR.Unchecked += (o, e) =>
|
||||
{
|
||||
Settings.Instance.ReplaceWinR = false;
|
||||
Settings.Instance.SaveSettings();
|
||||
};
|
||||
}
|
||||
|
||||
private void Setting_Loaded(object sender, RoutedEventArgs e)
|
||||
@@ -27,6 +37,7 @@ namespace WinAlfred
|
||||
}
|
||||
|
||||
themeComboBox.SelectedItem = Settings.Instance.Theme;
|
||||
cbReplaceWinR.IsChecked = Settings.Instance.ReplaceWinR;
|
||||
}
|
||||
|
||||
private List<string> LoadAvailableThemes()
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<UseVSHostingProcess>false</UseVSHostingProcess>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
@@ -77,6 +78,9 @@
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Accessibility" />
|
||||
<Reference Include="InputSimulator">
|
||||
<HintPath>C:\Users\Scott\Desktop\InputSimulator.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="log4net">
|
||||
<HintPath>..\packages\log4net.2.0.3\lib\net35-full\log4net.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -114,10 +118,12 @@
|
||||
<Compile Include="Commands\SystemCommand.cs" />
|
||||
<Compile Include="DispatcherExtensions.cs" />
|
||||
<Compile Include="Helper\DwmDropShadow.cs" />
|
||||
<Compile Include="Helper\GlobalKeyboardHook.cs" />
|
||||
<Compile Include="Helper\KeyboardHook.cs" />
|
||||
<Compile Include="Helper\Log.cs" />
|
||||
<Compile Include="Helper\Settings.cs" />
|
||||
<Compile Include="Helper\WinAlfredException.cs" />
|
||||
<Compile Include="KeyboardListener.cs" />
|
||||
<Compile Include="Msg.xaml.cs">
|
||||
<DependentUpon>Msg.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user