Blog型メモ帳 Ver.2019.08.20

ダラダラとそこはかとなく。Webメモ帳。日記。内容無し駄文。Wired思考アーカイブ。(1999/1/7~)

Windows10のキーボードロックダウン方法

Windows10のキーボードロックダウン方法
組み込み系等で使用する場合、キーボードの特定のキー操作を無効にしたい場合がある。これらを実現するためには以下の方法で行う。

【実行環境について】
コントロールパネル>全てのコントロールパネル項目>プログラムと機能
Windowsの機能の有効化または無効化>デバイスのロックダウン
からキーボードフィルタを有効にしてください。
次に、スタートメニューの中にある「Windows Powershell」を右クリックして
管理者権限で実行してください。

PowerShellに①の例文を貼り付けて実行するとキーボードのキー操作を無効化できます。PCを再起動しても機能は残ります。②は無効になったキー操作を有効化します。


  
①winキーとalt ctrl delを禁止する例文。


ここから
#
# Copyright (C) Microsoft. All rights reserved.
#

<#
.Synopsis
    This script shows how to use the built in WMI providers to enable and add 
    keyboard filter rules through Windows PowerShell on the local computer.
.Parameter ComputerName
    Optional parameter to specify a remote machine that this script should
    manage.  If not specified, the script will execute all WMI operations
    locally.
#>
param (
    [String] $ComputerName
)

$CommonParams = @{"namespace"="root\standardcimv2\embedded"}
$CommonParams += $PSBoundParameters

function Enable-Predefined-Key($Id) {
    <#
    .Synopsis
        Toggle on a Predefined Key keyboard filter Rule
    .Description
        Use Get-WMIObject to enumerate all WEKF_PredefinedKey instances,
        filter against key value "Id", and set that instance's "Enabled"
        property to 1/true.
    .Example
        Enable-Predefined-Key "Ctrl+Alt+Delete"
        Enable CAD filtering
#>

    $predefined = Get-WMIObject -class WEKF_PredefinedKey @CommonParams |
        where {
            $_.Id -eq "$Id"
        };

    if ($predefined) {
        $predefined.Enabled = 1;
        $predefined.Put() | Out-Null;
        Write-Host Enabled $Id
    } else {
        Write-Error $Id is not a valid predefined key
    }
}

# Some example uses of the functions defined above.
Enable-Predefined-Key "Ctrl+Alt+Del"
Enable-Predefined-Key "Windows"
ここまで


 テキストの内容をPowershellに貼り付けて実行すると、WindowsキーとAlt+Ctrl+Delが無効になります。無効化の処理をする場所は一番最後の行でこの場合、
Enable-Predefined-Key "Ctrl+Alt+Del"
Enable-Predefined-Key "Windows"
の2行です。
他のキーを無効化したい場合は以下のルールに沿って追加する。


このコマンドを実行すると再起動してもキーは無効化されたままです。
②を実行する事でキーの無効化を解除できます。


②key無効を解除する例文
ここから

#
# Copyright (C) Microsoft. All rights reserved.
#

<#
.Synopsis
    This Windows PowerShell script shows how to enumerate all existing keyboard filter
    rules and how to disable them by setting the Enabled property directly.
.Description
    For each instance of WEKF_PredefinedKey, WEKF_CustomKey, and WEKF_Scancode,
    set the Enabled property to false/0 to disable the filter rule, thus
    allowing all key sequences through the filter.
.Parameter ComputerName
    Optional parameter to specify the remote computer that this script should
    manage.  If not specified, the script will execute all WMI operations
    locally.
#>

param(
    [String]$ComputerName
)

$CommonParams = @{"namespace"="root\standardcimv2\embedded"}
$CommonParams += $PSBoundParameters

Get-WMIObject -class WEKF_PredefinedKey @CommonParams |
    foreach {
        if ($_.Enabled) {
            $_.Enabled = 0;
            $_.Put() | Out-Null;
            Write-Host Disabled $_.Id
        }
    }

Get-WMIObject -class WEKF_CustomKey @CommonParams |
    foreach {
        if ($_.Enabled) {
            $_.Enabled = 0;
            $_.Put() | Out-Null;
            Write-Host Disabled $_.Id
        }
    }

Get-WMIObject -class WEKF_Scancode @CommonParams |
    foreach {
        if ($_.Enabled) {
            $_.Enabled = 0;
            $_.Put() | Out-Null;
            "Disabled {0}+{1:X4}" -f $_.Modifiers,$_.Scancode
        }
    }
ここまで


 無効化したキーを再び使いたい場合はテキストの内容をPowerShellで実行してください。再びキーが使用できるようになります。


あと、レジストリにキーボードのロックを有効にする、無効にする、という項目が存在しますが、初期状態だとここを変更しても何故かキーがロックされません。これはキーボードロックダウンのサービスが有効化されていないためで、①のスクリプトでそれが有効にされます。これはWindowsのサービスを確認することで、自動起動になっていることが確認できます。そして、その後はレジストリの値を書き換えることでキーボードロックの有効/無効を切り替えることができるようになります。以下のリンク先にもありますが、
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Embedded\KeyboardFilter
のところです。
ここの方はレジストリのみを変更したのでキーボードロックが有効になっていません。その理由は上記に示したとおりです。


その他マイクロソフトの資料