perf: add keydown to textbox

main
DESKTOP-3BO4HSG\ksat 2 years ago
parent 74eb8755a5
commit 84ea248601

@ -120,127 +120,118 @@
<StackPanel Margin="20"> <StackPanel Margin="20">
<GroupBox Header="快捷键"> <GroupBox Header="快捷键">
<Grid Margin="20,10,20,10"> <Grid Margin="20,10,20,10">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="100*"/> <ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Label Content="划词翻译" <Label Content="划词翻译"
VerticalAlignment="Center" VerticalAlignment="Center"
Grid.Row="1" Grid.Row="1"
Grid.Column="0" Grid.Column="0"
Margin="0,5,0,0" /> Margin="0,5,0,0" />
<TextBox x:Name="CrossWord_TextBox" <TextBox Cursor="Arrow"
Cursor="Arrow" Width="200"
Width="200" Grid.Row="1"
Grid.Row="1" Grid.Column="1"
Grid.Column="1" Margin="0,5,0,0"
Margin="0,5,0,0" CaretBrush="Transparent"
CaretBrush="Transparent" VerticalContentAlignment="Center"
VerticalContentAlignment="Center" InputMethod.IsInputMethodEnabled="False"
InputMethod.IsInputMethodEnabled="False"> PreviewKeyDown="HotKeyTextBox_PreviewKeyDown"
<i:Interaction.Triggers> KeyUp="CrossWord_KeyUp"/>
<i:EventTrigger EventName="PreviewKeyDown"> <Label Grid.Row="1"
<i:InvokeCommandAction Command="{Binding PreviewKeyDownCmd}" Grid.Column="2"
CommandParameter="{Binding ElementName=CrossWord_TextBox}"/> Margin="0,5,0,0"
</i:EventTrigger> Content="热键冲突"
<i:EventTrigger EventName="KeyUp"> VerticalAlignment="Center"
<i:InvokeCommandAction Command="{Binding CrossWordKeyUpCmd}"/> Visibility="Hidden"
</i:EventTrigger> Foreground="Red" />
</i:Interaction.Triggers>
</TextBox>
<Label Grid.Row="1"
Grid.Column="2"
Margin="0,5,0,0"
Content="热键冲突"
VerticalAlignment="Center"
Visibility="Hidden"
Foreground="Red" />
<Label Content="截图翻译" <Label Content="截图翻译"
VerticalAlignment="Center" VerticalAlignment="Center"
Grid.Row="2" Grid.Row="2"
Grid.Column="0" Grid.Column="0"
Margin="0,5,0,0" /> Margin="0,5,0,0" />
<TextBox Cursor="Arrow" <TextBox Cursor="Arrow"
Width="200" Width="200"
Grid.Row="2" Grid.Row="2"
Grid.Column="1" Grid.Column="1"
Margin="0,5,0,0" Margin="0,5,0,0"
CaretBrush="Transparent" CaretBrush="Transparent"
VerticalContentAlignment="Center" VerticalContentAlignment="Center"
InputMethod.IsInputMethodEnabled="False"/> InputMethod.IsInputMethodEnabled="False"/>
<Label Grid.Row="2" <Label Grid.Row="2"
Grid.Column="2" Grid.Column="2"
Margin="0,5,0,0" Margin="0,5,0,0"
Content="热键冲突" Content="热键冲突"
VerticalAlignment="Center" VerticalAlignment="Center"
Visibility="Hidden" Visibility="Hidden"
Foreground="Red" /> Foreground="Red" />
<Label Content="输入翻译" <Label Content="输入翻译"
VerticalAlignment="Center" VerticalAlignment="Center"
Grid.Row="3" Grid.Row="3"
Grid.Column="0" Grid.Column="0"
Margin="0,5,0,0" /> Margin="0,5,0,0" />
<TextBox Cursor="Arrow" <TextBox Cursor="Arrow"
Width="200" Width="200"
Grid.Row="3" Grid.Row="3"
Grid.Column="1" Grid.Column="1"
Margin="0,5,0,0" Margin="0,5,0,0"
CaretBrush="Transparent" CaretBrush="Transparent"
VerticalContentAlignment="Center" VerticalContentAlignment="Center"
InputMethod.IsInputMethodEnabled="False"/> InputMethod.IsInputMethodEnabled="False"/>
<Label Grid.Row="3" <Label Grid.Row="3"
Grid.Column="2" Grid.Column="2"
Margin="0,5,0,0" Margin="0,5,0,0"
Content="热键冲突" Content="热键冲突"
VerticalAlignment="Center" VerticalAlignment="Center"
Visibility="Hidden" Visibility="Hidden"
Foreground="Red" /> Foreground="Red" />
<Label Content="显示界面" <Label Content="显示界面"
VerticalAlignment="Center" VerticalAlignment="Center"
Grid.Row="4" Grid.Row="4"
Grid.Column="0" Grid.Column="0"
Margin="0,5,0,0" /> Margin="0,5,0,0" />
<TextBox Cursor="Arrow" <TextBox Cursor="Arrow"
Width="200" Width="200"
Grid.Row="4" Grid.Row="4"
Grid.Column="1" Grid.Column="1"
Margin="0,5,0,0" Margin="0,5,0,0"
CaretBrush="Transparent" CaretBrush="Transparent"
VerticalContentAlignment="Center" VerticalContentAlignment="Center"
InputMethod.IsInputMethodEnabled="False"/> InputMethod.IsInputMethodEnabled="False"/>
<Label Grid.Row="4" <Label Grid.Row="4"
Grid.Column="2" Grid.Column="2"
Margin="0,5,0,0" Margin="0,5,0,0"
Content="热键冲突" Content="热键冲突"
VerticalAlignment="Center" VerticalAlignment="Center"
Visibility="Hidden" Visibility="Hidden"
Foreground="Red" /> Foreground="Red" />
<Button Content="恢复默认" <Button Content="恢复默认"
Template="{StaticResource buttonstyle}" Template="{StaticResource buttonstyle}"
Width="80" Width="80"
Grid.Row="5" Grid.Row="5"
Margin="5" Margin="5"
Grid.Column="1" Grid.Column="1"
Command="{Binding ResetHotKeysCmd}"/> Command="{Binding ResetHotKeysCmd}"/>
</Grid> </Grid>
</GroupBox> </GroupBox>
</StackPanel> </StackPanel>

@ -29,5 +29,55 @@ namespace STranslate.View
DataContext = ViewModel.SettingsVM.Instance; DataContext = ViewModel.SettingsVM.Instance;
} }
private byte _hotkeysModifiers;
private int _hotkeysKey;
private string _hotkeysText = string.Empty;
private void HotKeyTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
_hotkeysModifiers = 0;
_hotkeysKey = 0;
_hotkeysText = "";
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
if (key == Key.LeftShift || key == Key.RightShift
|| key == Key.LeftCtrl || key == Key.RightCtrl
|| key == Key.LeftAlt || key == Key.RightAlt
|| key == Key.LWin || key == Key.RWin)
{
return;
}
StringBuilder shortcutText = new StringBuilder();
if ((Keyboard.Modifiers & ModifierKeys.Control) != 0)
{
_hotkeysModifiers += 2;
shortcutText.Append("Ctrl + ");
}
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0)
{
_hotkeysModifiers += 4;
shortcutText.Append("Shift + ");
}
if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0)
{
_hotkeysModifiers += 1;
shortcutText.Append("Alt + ");
}
if (_hotkeysModifiers == 0 && (key < Key.F1 || key > Key.F12))
{
_hotkeysKey = 0;
shortcutText.Clear();
((System.Windows.Controls.TextBox)sender).Text = _hotkeysText = "";
return;
}
_hotkeysKey = KeyInterop.VirtualKeyFromKey(key);
shortcutText.Append(key.ToString());
((System.Windows.Controls.TextBox)sender).Text = _hotkeysText = shortcutText.ToString();
}
private void CrossWord_KeyUp(object sender, KeyEventArgs e)
{
}
} }
} }

@ -91,16 +91,6 @@ namespace STranslate.ViewModel
MessageBox.Show($"未找到默认应用\n{ex.Message}"); MessageBox.Show($"未找到默认应用\n{ex.Message}");
} }
}); });
PreviewKeyDownCmd = new RelayCommand((_) => true, (o) =>
{
System.Diagnostics.Debug.Print($"Press key, control: {o}");
});
CrossWordKeyUpCmd = new RelayCommand((_) => true, (_) =>
{
System.Diagnostics.Debug.Print($"Key up");
});
} }
@ -125,11 +115,6 @@ namespace STranslate.ViewModel
public ICommand StartupCmd { get; private set; } public ICommand StartupCmd { get; private set; }
public ICommand EscCmd { get; private set; } public ICommand EscCmd { get; private set; }
public ICommand ResetHotKeysCmd { get; private set; } public ICommand ResetHotKeysCmd { get; private set; }
/// <summary>
/// 按键按下事件
/// </summary>
public ICommand PreviewKeyDownCmd { get; private set; }
public ICommand CrossWordKeyUpCmd { get; private set; }
private static SettingsVM _instance; private static SettingsVM _instance;

Loading…
Cancel
Save