-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
142 lines (116 loc) · 5.07 KB
/
MainWindow.xaml.cs
File metadata and controls
142 lines (116 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using Microsoft.Win32;
using System.Reflection;
using System.ComponentModel;
namespace NormalKeyboardSwitcher
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private KeyboardListener keyboardListener;
private ForegroundWindowListener foregroundWindowListener;
private InputController inputController;
private RegistryKey autorunKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
private Assembly curAssembly = Assembly.GetExecutingAssembly();
public MainWindow()
{
InitializeComponent();
inputController = new InputController();
keyboardListener = new KeyboardListener(FirstKey.Control);
foregroundWindowListener = new ForegroundWindowListener();
// when languange change key combinations are pressed then call appropriate methods of input controller
keyboardListener.NextTemporaryInputLanguage += inputController.NextTemporaryInputLanguage;
keyboardListener.SwitchToTemporaryInputLanguage += inputController.SwitchToTemporaryInputLanguage;
keyboardListener.DropTemporaryInputLanguage += inputController.DropTemporaryInputLanguage;
// if input language is changed in input controller, then send it to foreground window
inputController.InputLanguageChanged += foregroundWindowListener.InputLangChangeRequestFocused;
// if foreground window changed, then send to it a request to change to current language from the controller
foregroundWindowListener.ForegroundWindowChanged += (hwnd) => {
//foregroundWindowListener.InputLangChangeRequest(hwnd, inputController.UsedInputLanguage);
// will notify only focused
foregroundWindowListener.InputLangChangeRequestFocused(inputController.UsedInputLanguage);
};
// binds list of system languages to a list box in GUI
LanguagesListBox.ItemsSource = inputController.UsedInputLanguages;
Binding TemporaryInputLanguageBinding = new Binding("TemporaryInputLanguage");
TemporaryInputLanguageBinding.Mode = BindingMode.OneWay;
TemporaryInputLanguageBinding.Source = inputController;
LanguagesListBox.SetBinding(ListBox.SelectedIndexProperty, TemporaryInputLanguageBinding);
// settin initial value of autorun checkbox
AutorunCheckBox.IsChecked = Autorun;
}
bool Autorun
{
get
{
object oldAutorunValue = autorunKey.GetValue(curAssembly.GetName().Name);
return oldAutorunValue != null && oldAutorunValue.Equals(curAssembly.Location);
}
set
{
if (value && !Autorun)
{
autorunKey.SetValue(curAssembly.GetName().Name, curAssembly.Location);
}
else if( !value && Autorun )
{
autorunKey.DeleteValue(curAssembly.GetName().Name, false);
}
}
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState == System.Windows.WindowState.Minimized)
Hide();
base.OnStateChanged(e);
}
protected override void OnClosing(CancelEventArgs e)
{
MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?\nNormal Keyboard Switcher won't switch keyboards anymore.\nIf you want to hide it's window then select 'No' here and minimize it's window",
"Exit Confirmation", System.Windows.MessageBoxButton.YesNo);
if (messageBoxResult != MessageBoxResult.Yes) {
e.Cancel = true;
}
base.OnClosing(e);
}
private void SwitchButton_Click(object sender, RoutedEventArgs e)
{
if(LanguagesListBox.SelectedIndex>=0)
{
inputController.SwitchToLanguage(LanguagesListBox.SelectedIndex);
}
}
private void AutorunCheckBox_CheckedChanged(object sender, RoutedEventArgs e)
{
if (AutorunCheckBox.IsChecked != null)
{
Autorun = (bool)AutorunCheckBox.IsChecked;
}
}
private void OpenMenuItem_Click(object sender, RoutedEventArgs e)
{
Show();
WindowState = WindowState.Normal;
}
private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}