-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
81 lines (58 loc) · 2.18 KB
/
MainWindow.xaml.cs
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
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using NAudio.Wave;
namespace H_Writes
{
public partial class MainWindow : Window
{
private int letterSound = 0; // Index to track the current typing sound
private string[] typingSounds = { "first.wav", "second.wav", "third.wav" }; // Array of typing sound filenames
private WaveOut waveOut; // Audio playback controller
public MainWindow()
{
InitializeComponent();
// Initialize the audio playback controller
waveOut = new WaveOut();
var audioFile = new AudioFileReader(typingSounds[letterSound]);
waveOut.Init(audioFile);
// Event handler for key presses
this.KeyDown += MainWindow_KeyDown;
// Set the cursor and focus to the text input
this.Cursor = Cursors.None;
TextInput.Focus();
}
private async void TextInput_KeyUp(object sender, KeyEventArgs e)
{
await PlayTypingSoundAsync(); // Play typing sound asynchronously on key up event
}
private async Task PlayTypingSoundAsync()
{
var audioFile = new AudioFileReader(typingSounds[letterSound]);
// Configure audio playback
waveOut.DeviceNumber = -1;
waveOut.Init(audioFile);
int delayDuration = 10;
// Play the sound asynchronously after a delay
var playTask = Task.Run(async () =>
{
await Task.Delay(delayDuration);
Dispatcher.Invoke(() => waveOut.Play());
});
await playTask;
// Update the typing sound index
letterSound = (letterSound + 1) % typingSounds.Length;
}
private void ExitFullscreen()
{
this.WindowState = WindowState.Normal;
this.WindowStyle = WindowStyle.SingleBorderWindow;
}
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
this.Close(); // Close the application on Escape key press
}
}
}