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.