How to diable keyboard

Raman Singh 0 Reputation points
2024-06-08T14:59:37.5266667+00:00

I want to disable keyboard using toggle switch and these are my code I don't know what the command is to disable keyboard using toggle Switch.
please help me I am beginner in Win UI (C++/CX & C++/WinRT).

<! --xaml -->
<ToggleSwitch IsOn="True">ToogleSwitch</ToggleSwitch>
//xaml.cpp
bool switchIsOn;
void MainWindow::ToogleSwitch(bool IsOn)
{
    switchIsOn = IsOn;
    if (switchIsOn == true)
    {
        
    }
}
//xaml.h
void ToogleSwitch(bool IsOn);
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,596 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
778 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,630 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Minxin Yu 10,461 Reputation points Microsoft Vendor
    2024-06-10T03:14:46.3733333+00:00

    Hi, @Raman Singh

    You could use hook.

          <ToggleSwitch x:Name="MyToggleSwitch" Header="Toggle Switch Example"  Toggled="ToggleSwitch_Toggled"/>
          <TextBox x:Name="MyTextBox" Header="Enter text here:" PlaceholderText="Type something" Width="300"  />
    

    header file

    #pragma once
    
    #include <windows.h>
    #include "MainWindow.g.h"
    #pragma comment( lib, "user32.lib" )
    
    static HHOOK g_keyboardHook =nullptr;
    LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam);
    namespace winrt::App1::implementation
    {
      
        struct MainWindow : MainWindowT<MainWindow>
        {
         
         
            MainWindow()
            {
               
             
               
            };
            ~MainWindow()
            {
               
                if (g_keyboardHook != nullptr)
                {
                    UnhookWindowsHookEx(g_keyboardHook);
                    g_keyboardHook = nullptr;
                }
            };
            int32_t MyProperty();
            void MyProperty(int32_t value);
            void myButton_Click(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
            void ToggleSwitch_Toggled(IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args)
            {
                if (MyToggleSwitch().IsOn())
                {
                    g_keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, nullptr, 0);
                }
                else
                {
                    if (g_keyboardHook != nullptr)
                    {
                        UnhookWindowsHookEx(g_keyboardHook);
                        g_keyboardHook = nullptr;
                    }
                }
            }
        
        };
    }
    namespace winrt::App1::factory_implementation
    {
        struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow>
        {
        };
    }
    

    cpp:

    ......
    LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
        if (nCode >= 0)
        {
            if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)
            {
                return 1;
            }
        }
        return CallNextHookEx(g_keyboardHook, nCode, wParam, lParam);
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.