Skip to content

Commit f85373a

Browse files
Update setup.ps1
sticky keys
1 parent 8a3ae19 commit f85373a

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

setup.ps1

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# FUNCTIONS
22

3-
function CREATE_BIN { # Create bin if it does not exist
3+
function CREATE_BIN {
44
if (!(Test-Path "$HOME\bin")) {
55
Write-Output "`nBin does not exist. Creating."
66
mkdir $HOME\bin
@@ -30,6 +30,85 @@ public static void UpdateUserPreferencesMask() {
3030
[User32.UserPreferencesMaskSPI]::UpdateUserPreferencesMask()
3131
}
3232

33+
Add-Type -Namespace demo -Name StickyKeys -MemberDefinition '
34+
[DllImport("user32.dll", SetLastError = true)]
35+
[return: MarshalAs(UnmanagedType.Bool)]
36+
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref STICKYKEYS pvParam, uint fWinIni);
37+
38+
[StructLayout(LayoutKind.Sequential)]
39+
struct STICKYKEYS {
40+
public uint cbSize;
41+
public UInt32 dwFlags;
42+
}
43+
44+
[Flags]
45+
public enum StickyKeyFlags : uint {
46+
AUDIBLEFEEDBACK = 0x00000040,
47+
AVAILABLE = 0x00000002,
48+
CONFIRMHOTKEY = 0x00000008,
49+
HOTKEYACTIVE = 0x00000004,
50+
HOTKEYSOUND = 0x00000010,
51+
INDICATOR = 0x00000020,
52+
STICKYKEYSON = 0x00000001,
53+
TRISTATE = 0x00000080,
54+
TWOKEYSOFF = 0x00000100,
55+
LALTLATCHED = 0x10000000,
56+
LCTLLATCHED = 0x04000000,
57+
LSHIFTLATCHED = 0x01000000,
58+
RALTLATCHED = 0x20000000,
59+
RCTLLATCHED = 0x08000000,
60+
RSHIFTLATCHED = 0x02000000,
61+
LALTLOCKED = 0x00100000,
62+
LCTLLOCKED = 0x00040000,
63+
LSHIFTLOCKED = 0x00010000,
64+
RALTLOCKED = 0x00200000,
65+
RCTLLOCKED = 0x00080000,
66+
RSHIFTLOCKED = 0x00020000,
67+
LWINLATCHED = 0x40000000,
68+
RWINLATCHED = 0x80000000,
69+
LWINLOCKED = 0x00400000,
70+
RWINLOCKED = 0x00800000
71+
}
72+
73+
public static bool IsHotKeyEnabled {
74+
get { return (GetFlags() & StickyKeyFlags.HOTKEYACTIVE) != 0u; }
75+
set { EnableHotKey(value, false); }
76+
}
77+
78+
public static StickyKeyFlags ActiveFlags {
79+
get { return GetFlags(); }
80+
set { SetFlags(value, false); }
81+
}
82+
83+
// The flags in effect on a pristine system.
84+
public static StickyKeyFlags DefaultFlags {
85+
get { return StickyKeyFlags.AVAILABLE | StickyKeyFlags.HOTKEYACTIVE | StickyKeyFlags.CONFIRMHOTKEY | StickyKeyFlags.HOTKEYSOUND | StickyKeyFlags.INDICATOR | StickyKeyFlags.AUDIBLEFEEDBACK | StickyKeyFlags.TRISTATE | StickyKeyFlags.TWOKEYSOFF; } // 510u
86+
}
87+
88+
public static void EnableHotKey(bool enable = true, bool persist = false) {
89+
var skInfo = new STICKYKEYS();
90+
skInfo.cbSize = (uint)Marshal.SizeOf(skInfo);
91+
var flags = GetFlags();
92+
SetFlags((enable ? flags | StickyKeyFlags.HOTKEYACTIVE : flags & ~StickyKeyFlags.HOTKEYACTIVE), persist);
93+
}
94+
95+
private static StickyKeyFlags GetFlags() {
96+
var skInfo = new STICKYKEYS();
97+
skInfo.cbSize = (uint)Marshal.SizeOf(skInfo);
98+
if (!SystemParametersInfo(0x003a /* SPI_GETSTICKYKEYS */, 0, ref skInfo, 0))
99+
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
100+
return (StickyKeyFlags)skInfo.dwFlags;
101+
}
102+
103+
public static void SetFlags(StickyKeyFlags flags, bool persist = false) {
104+
var skInfo = new STICKYKEYS();
105+
skInfo.cbSize = (uint)Marshal.SizeOf(skInfo);
106+
skInfo.dwFlags = (UInt32)flags;
107+
if (!SystemParametersInfo(0x003b /* SPI_SETSTICKYKEYS */, 0, ref skInfo, persist ? 1u : 0u))
108+
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
109+
}
110+
'
111+
33112
# SCRIPT
34113

35114
Write-Output "`n!!!!!!!!!!`nWARNING: THIS SCRIPT MAKE CHANGES TO THE REGISTRY, MAKE SURE YOU HAVE MADE A RESTORE POINT`n!!!!!!!!!!`n"
@@ -540,6 +619,7 @@ public class Wallpaper
540619
# Print choices
541620
Write-Output "`n1. Change the keyboard layout"
542621
Write-Output "2. Change the mouse speed"
622+
Write-Output "3. Edit sticky keys"
543623
# Prompt user for choice
544624
$Iset = Read-Host "`nInput the number of an option from the list above, or leave blank to exit"
545625
switch ($Iset) {
@@ -592,6 +672,20 @@ public class Wallpaper
592672
}
593673
} until ($MouseSpeed -In 1..20 -Or $MouseSpeed -notmatch "\S")
594674
}
675+
3 { # Edit sticky keys
676+
$SKEYS = $Host.UI.PromptForChoice("Enable the sticky keys hotkey?", "", @("&Cancel", "&Enable", "&Disable"), 0)
677+
switch ($SKEYS) {
678+
0 {
679+
Write-Output "`nCanceled"
680+
}
681+
1 {
682+
[demo.StickyKeys]::EnableHotKey($false, $true)
683+
}
684+
2 {
685+
[demo.StickyKeys]::EnableHotKey($true, $true)
686+
}
687+
}
688+
}
595689
}
596690
} until ($Iset -notmatch "\S")
597691
}
@@ -607,7 +701,7 @@ public class Wallpaper
607701
switch ($PGram) {
608702
1 { # FireFox
609703
$InstallFirefox = $Host.UI.PromptForChoice("Which version of firefox would you like to install?", "", @("&Cancel", "&Latest", "&Nightly", "&Beta", "&Dev", "&ESR"), 0)
610-
switch ($InstallFirefox -eq 1) {
704+
switch ($InstallFirefox) {
611705
0 { # Cancel
612706
Write-Output "`nCanceled"
613707
}

0 commit comments

Comments
 (0)