PowerShell İle Klavye/Fare Hızı Ayarlayın

Programlama ve Script dilleri konusunda bilgi paylaşım alanıdır.
Cevapla
Kullanıcı avatarı
TRWE_2012
Zettabyte1
Zettabyte1
Mesajlar: 15518
Kayıt: 25 Eyl 2013, 13:38
cinsiyet: Erkek
Teşekkür etti: 2684 kez
Teşekkür edildi: 5558 kez

PowerShell İle Klavye/Fare Hızı Ayarlayın

Mesaj gönderen TRWE_2012 »

Merhabalar

Mouse Keyboard Speed Settings.ps1

Kod: Tümünü seç

# ============================================================
# Mouse and Keyboard Speed Settings
# System : Windows 11
# ============================================================

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

# --- P/Invoke for applying settings immediately (equivalent of xset) ---
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class NativeInput {
    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
}
"@

$SPI_SETMOUSESPEED = 0x0071
$SPI_SETKEYBOARDSPEED = 0x000B
$SPI_SETKEYBOARDDELAY = 0x0017
$SPIF_UPDATEINIFILE = 0x01
$SPIF_SENDCHANGE = 0x02

# --- DEFAULT VALUES (chosen sensible defaults, within Windows' native ranges) ---
$DEFAULT_MOUSE_SPEED = 10          # Range: 1-20
$DEFAULT_KEYBOARD_REPEAT_RATE = 31 # Range: 0-31
$DEFAULT_KEYBOARD_DELAY = 1        # Range: 0-3 (maps to 250/500/750/1000 ms)

# --- CONFIG FILE PATH ---
$ConfigFile = Join-Path $env:USERPROFILE ".mouse_keyboard_settings.conf"

function Show-ErrorMessage {
    param([string]$Message)
    [System.Windows.Forms.MessageBox]::Show($Message, "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null
}

function Show-SuccessMessage {
    param([string]$Message)
    [System.Windows.Forms.MessageBox]::Show($Message, "Success", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information) | Out-Null
}

# --- LOAD EXISTING SETTINGS OR USE DEFAULTS ---
$MouseSpeed = $DEFAULT_MOUSE_SPEED
$KeyboardRepeatRate = $DEFAULT_KEYBOARD_REPEAT_RATE
$KeyboardDelay = $DEFAULT_KEYBOARD_DELAY

if (Test-Path -LiteralPath $ConfigFile) {
    Get-Content -LiteralPath $ConfigFile | ForEach-Object {
        if ($_ -match '^MOUSE_SPEED=(\d+)$') { $MouseSpeed = [int]$Matches[1] }
        if ($_ -match '^KEYBOARD_REPEAT_RATE=(\d+)$') { $KeyboardRepeatRate = [int]$Matches[1] }
        if ($_ -match '^KEYBOARD_DELAY=(\d+)$') { $KeyboardDelay = [int]$Matches[1] }
    }
}

function Save-Settings {
    $lines = @(
        "MOUSE_SPEED=$MouseSpeed"
        "KEYBOARD_REPEAT_RATE=$KeyboardRepeatRate"
        "KEYBOARD_DELAY=$KeyboardDelay"
    )
    Set-Content -LiteralPath $ConfigFile -Value $lines -Encoding UTF8
}

# --- ASK WHETHER THE USER WANTS TO CHANGE SETTINGS ---
$startChoice = [System.Windows.Forms.MessageBox]::Show(
    "Do you want to change your Mouse and Keyboard speed?",
    "Change Settings",
    [System.Windows.Forms.MessageBoxButtons]::YesNo,
    [System.Windows.Forms.MessageBoxIcon]::Question
)
if ($startChoice -ne [System.Windows.Forms.DialogResult]::Yes) {
    exit 0
}

# -------------------------------
# Settings form (sliders)
# -------------------------------

function Show-SettingsForm {
    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Mouse and Keyboard Settings"
    $form.Width = 420
    $form.Height = 340
    $form.StartPosition = "CenterScreen"
    $form.FormBorderStyle = "FixedDialog"
    $form.MaximizeBox = $false

    # --- Mouse Speed ---
    $mouseLabel = New-Object System.Windows.Forms.Label
    $mouseLabel.Text = "Mouse Speed (1 - 20): $MouseSpeed"
    $mouseLabel.Location = New-Object System.Drawing.Point(15, 15)
    $mouseLabel.Width = 350
    $form.Controls.Add($mouseLabel)

    $mouseSlider = New-Object System.Windows.Forms.TrackBar
    $mouseSlider.Minimum = 1
    $mouseSlider.Maximum = 20
    $mouseSlider.Value = $MouseSpeed
    $mouseSlider.Location = New-Object System.Drawing.Point(15, 40)
    $mouseSlider.Width = 370
    $mouseSlider.Add_Scroll({ $mouseLabel.Text = "Mouse Speed (1 - 20): $($mouseSlider.Value)" })
    $form.Controls.Add($mouseSlider)

    # --- Keyboard Repeat Rate ---
    $repeatLabel = New-Object System.Windows.Forms.Label
    $repeatLabel.Text = "Keyboard Repeat Rate (0 - 31): $KeyboardRepeatRate"
    $repeatLabel.Location = New-Object System.Drawing.Point(15, 100)
    $repeatLabel.Width = 350
    $form.Controls.Add($repeatLabel)

    $repeatSlider = New-Object System.Windows.Forms.TrackBar
    $repeatSlider.Minimum = 0
    $repeatSlider.Maximum = 31
    $repeatSlider.Value = $KeyboardRepeatRate
    $repeatSlider.Location = New-Object System.Drawing.Point(15, 125)
    $repeatSlider.Width = 370
    $repeatSlider.Add_Scroll({ $repeatLabel.Text = "Keyboard Repeat Rate (0 - 31): $($repeatSlider.Value)" })
    $form.Controls.Add($repeatSlider)

    # --- Keyboard Delay ---
    $delayLabel = New-Object System.Windows.Forms.Label
    $delayMsMap = @{ 0 = 250; 1 = 500; 2 = 750; 3 = 1000 }
    $delayLabel.Text = "Keyboard Delay (0 - 3): $KeyboardDelay ($($delayMsMap[$KeyboardDelay]) ms)"
    $delayLabel.Location = New-Object System.Drawing.Point(15, 185)
    $delayLabel.Width = 350
    $form.Controls.Add($delayLabel)

    $delaySlider = New-Object System.Windows.Forms.TrackBar
    $delaySlider.Minimum = 0
    $delaySlider.Maximum = 3
    $delaySlider.Value = $KeyboardDelay
    $delaySlider.Location = New-Object System.Drawing.Point(15, 210)
    $delaySlider.Width = 370
    $delaySlider.Add_Scroll({ $delayLabel.Text = "Keyboard Delay (0 - 3): $($delaySlider.Value) ($($delayMsMap[$delaySlider.Value]) ms)" })
    $form.Controls.Add($delaySlider)

    # --- Buttons ---
    $okButton = New-Object System.Windows.Forms.Button
    $okButton.Text = "Continue"
    $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $okButton.Location = New-Object System.Drawing.Point(210, 260)
    $okButton.Width = 85
    $form.Controls.Add($okButton)

    $cancelButton = New-Object System.Windows.Forms.Button
    $cancelButton.Text = "Cancel"
    $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $cancelButton.Location = New-Object System.Drawing.Point(300, 260)
    $cancelButton.Width = 85
    $form.Controls.Add($cancelButton)

    $form.AcceptButton = $okButton
    $form.CancelButton = $cancelButton

    $result = $form.ShowDialog()

    if ($result -ne [System.Windows.Forms.DialogResult]::OK) {
        return $null
    }

    return [PSCustomObject]@{
        MouseSpeed          = $mouseSlider.Value
        KeyboardRepeatRate  = $repeatSlider.Value
        KeyboardDelay       = $delaySlider.Value
    }
}

$selection = Show-SettingsForm

if (-not $selection) {
    exit 0
}

$MouseSpeed = $selection.MouseSpeed
$KeyboardRepeatRate = $selection.KeyboardRepeatRate
$KeyboardDelay = $selection.KeyboardDelay

$delayMsMap = @{ 0 = 250; 1 = 500; 2 = 750; 3 = 1000 }

# --- CONFIRM SETTINGS ---
$confirmChoice = [System.Windows.Forms.MessageBox]::Show(
    "Do you confirm the settings you selected?`nMouse Speed: $MouseSpeed`nKeyboard Repeat Rate: $KeyboardRepeatRate`nKeyboard Delay: $KeyboardDelay ($($delayMsMap[$KeyboardDelay]) ms)",
    "Confirm Settings",
    [System.Windows.Forms.MessageBoxButtons]::YesNo,
    [System.Windows.Forms.MessageBoxIcon]::Question
)

if ($confirmChoice -ne [System.Windows.Forms.DialogResult]::Yes) {
    $resetChoice = [System.Windows.Forms.MessageBox]::Show(
        "Do you want to return to default settings?",
        "Return to Defaults",
        [System.Windows.Forms.MessageBoxButtons]::YesNo,
        [System.Windows.Forms.MessageBoxIcon]::Question
    )
    if ($resetChoice -eq [System.Windows.Forms.DialogResult]::Yes) {
        $MouseSpeed = $DEFAULT_MOUSE_SPEED
        $KeyboardRepeatRate = $DEFAULT_KEYBOARD_REPEAT_RATE
        $KeyboardDelay = $DEFAULT_KEYBOARD_DELAY
        Show-SuccessMessage "Returned to default settings."
    }
}

# --- APPLY MOUSE SPEED ---
$mousePtr = [IntPtr]$MouseSpeed
$mouseOk = [NativeInput]::SystemParametersInfo($SPI_SETMOUSESPEED, 0, $mousePtr, ($SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE))
if (-not $mouseOk) {
    Show-ErrorMessage "An error occurred while setting mouse speed."
    exit 1
}

# --- APPLY KEYBOARD REPEAT RATE ---
$keyboardSpeedOk = [NativeInput]::SystemParametersInfo($SPI_SETKEYBOARDSPEED, [uint32]$KeyboardRepeatRate, [IntPtr]::Zero, ($SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE))
if (-not $keyboardSpeedOk) {
    Show-ErrorMessage "An error occurred while setting keyboard repeat rate."
    exit 1
}

# --- APPLY KEYBOARD DELAY ---
$keyboardDelayOk = [NativeInput]::SystemParametersInfo($SPI_SETKEYBOARDDELAY, [uint32]$KeyboardDelay, [IntPtr]::Zero, ($SPIF_UPDATEINIFILE -bor $SPIF_SENDCHANGE))
if (-not $keyboardDelayOk) {
    Show-ErrorMessage "An error occurred while setting keyboard delay."
    exit 1
}

# --- SAVE SETTINGS ---
Save-Settings

# --- SHOW SUCCESS MESSAGE ---
Show-SuccessMessage "Your Mouse and Keyboard speed was set successfully!`nMouse Speed: $MouseSpeed`nKeyboard Repeat Rate: $KeyboardRepeatRate`nKeyboard Delay: $KeyboardDelay ($($delayMsMap[$KeyboardDelay]) ms)"

Read-Host "`nPress ENTER to exit."
EKRAN GÖRÜNTÜSÜ :
Resim
Resim
Resim
SONUÇ:
Resim
Güle güle kullanın...
Cevapla

“Programlama ve Script dilleri” sayfasına dön