Powershell Disabilitare i Tasti della Tastiera

Facendo riferimento a questi due link: HowToGeek e Microsoft e con l’ausilio di questo software per individuare i codici esadecimali dei tasti ho prodotto il seguente codice in powershell

 

Disablekeys.ps1

Param(
		[ValidateSet("enable","disable")]
		[String] 
		$Parametro
	) 

if($Parametro = "enable")
{
	if(gp -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout" | Select-Object -ExpandProperty "Scancode Map")
	{
		$percs = @("HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout") | % { if($KeyName = gi -Path $_  | Select-Object -ExpandProperty Property | Where-Object {$_ -match "Scancode Map"}) {Set-ItemProperty -Path $_ -Name $KeyName -Value ([byte[]] (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF6,0x00,0x00,0x00,0x00,0x00,0x5B,0xE0,0x00,0x00,0x5C,0xE0,0x00,0x00,0x1D,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x47,0xE0,0x00,0x00,0x1D,0xE0,0x00,0x00,0x38,0xE0,0x00,0x00,0x3B,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x3D,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x41,0x00,0x00,0x00,0x42,0x00,0x00,0x00,0x43,0x00,0x00,0x00,0x43,0x00,0x00,0x00,0x44,0x00,0x00,0x00,0x57,0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,0x00)) -Type Binary}}
	}
	else
	{
	Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout" -Name "Scancode Map" -Value ([byte[]] (0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF6,0x00,0x00,0x00,0x00,0x00,0x5B,0xE0,0x00,0x00,0x5C,0xE0,0x00,0x00,0x1D,0x00,0x00,0x00,0x38,0x00,0x00,0x00,0x47,0xE0,0x00,0x00,0x1D,0xE0,0x00,0x00,0x38,0xE0,0x00,0x00,0x3B,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x3D,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x41,0x00,0x00,0x00,0x42,0x00,0x00,0x00,0x43,0x00,0x00,0x00,0x43,0x00,0x00,0x00,0x44,0x00,0x00,0x00,0x57,0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00,0x00)) -Type Binary
	}
	Restart-Computer -Force
}
else if($Parametro = "disable")
{
	Remove-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Keyboard Layout" -Name "Scancode Map"
	Restart-Computer -Force
}

Stringa utilizzata e adattata per powershell

00000000 00000000 F600 0000 0000 5BE0 0000 5CE0 0000 1D00 0000 3800 0000 47E0 0000 1DE0 0000 38E0 0000 3B00 0000 3C00 0000 3D00 0000 3E00 0000 3F00 0000 4000 0000 4100 0000 4200 0000 4300 0000 4300 0000 4400 0000 5700 0000 5800 0000 00000000

Dal Sito Microsoft:

Scan code mapper for keyboards

In Microsoft Windows operating systems, PS/2-compatible scan codes provided by an input device are converted into virtual keys, which are propagated through the system in the form of Windows messages. If a device produces an incorrect scan code for a certain key, the wrong virtual key message will be sent. This can be fixed by writing a filter driver that analyzes the scan codes generated by firmware and modifies the incorrect scan code to one understood by the system. However, this is a tedious process and can sometimes lead to severe problems, if errors exist in the kernel-level filter driver.

Windows 2000 and Windows XP include a new Scan Code Mapper, which provides a method that allows for mapping of scan codes. The scan code mappings for Windows are stored in the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout

Note There is also a Keyboard Layouts key (notice the plural form) under the Control key, but that key should not be modified.

In the Keyboard Layout key, the Scancode Map value must be added. This value is of type REG_BINARY (little Endian format) and has the data format specified in the following table.
Start offset (in bytes) Size (in bytes) Data
0 4 Header: Version Information
4 4 Header: Flags
8 4 Header: Number of Mappings
12 4 Individual Mapping
… … …
Last 4 bytes 4 Null Terminator (0x00000000)

The first and second DWORDS store header information and should be set to all zeroes for the current version of the Scan Code Mapper. The third DWORD entry holds a count of the total number of mappings that follow, including the null terminating mapping. The minimum count would therefore be 1 (no mappings specified). The individual mappings follow the header. Each mapping is one DWORD in length and is divided into two WORD length fields. Each WORD field stores the scan code for a key to be mapped.

Once the map is stored in the registry, the system must be rebooted for the mappings to take effect. Note that if the mapping of a scan code is necessary on a keypress, the step is performed in user mode just before the scan code is converted to a virtual key. Doing this conversion in user mode can present certain limitations, such as mapping not working correctly when running under Terminal Services.

To remove these mappings, remove the Scancode Map registry value and reboot.

Example 1

The following presents an example. To swap the left CTRL key with the CAPS LOCK key, use a registry editor (preferably Regedt32.exe) to modify the Scancode Map key with the following value:

00000000 00000000 03000000 3A001D00 1D003A00 00000000

The following table contains these entries broken into DWORD fields and the bytes swapped.
Value Interpretation
0x00000000 Header: Version. Set to all zeroes.
0x00000000 Header: Flags. Set to all zeroes.
0x00000003 Three entries in the map (including null entry).
0x001D003A Left CTRL key –> CAPS LOCK (0x1D –> 0x3A).
0x003A001D CAPS LOCK –> Left CTRL key (0x3A –> 0x1D).
0x00000000 Null terminator.

Facendo riferimento alla stringa del esempio: 00000000 00000000 03000000 3A001D00 1D003A00 00000000

03000000: numero di tasti da rimappare +1
Ho messo F6 esadecimale = 21 decimale perchè ho disabilitato 20 tasti i tasti F i tasti windows e i tasti CTRL, ALT e Home

3A001D00 due tasti 3A00 1D00 sharpkeys restituisce codici del tipo 00_1D quindi i due tasti presi ad esempio verrebbero restituiti da sharpkeys nella forma 00_1D e 00_3A se invece di rimappare il CTRL Left volessimo disabilitarlo la stringa sarebbe 0000 1D00 quindi prima in sequenza si mette l’effetto che il tasto deve produrre o un’altro tasto o un’altra azione e poi il tasto da rimappare.
Per manipolare la stringa ho utilizzano Notepad++, ho tolto gli spazi e in fine CTRL+f “Replace” in “Search Mode” Regular expression senza check matches newline
Find what: (..)
Replace with: 0x$1,

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...