This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reactive.Linq; | |
using System.Reactive.Subjects; | |
namespace KeystrokeRedo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Create an observable sequence of keystrokes from the Console.KeyPress event | |
IObservable<string> keystrokes = Observable.FromEvent<ConsoleKeyInfo>( | |
handler => Console.KeyPress += handler, | |
handler => Console.KeyPress -= handler | |
).Select(key => key.KeyChar.ToString()); | |
// Create a subject that holds the last 10 keystrokes pressed | |
Subject<string> keystrokeBuffer = new Subject<string>(); | |
// Add keystrokes to the buffer when the button to redo keystrokes is pressed | |
keystrokes.Buffer(keystrokes.Where(key => key == "R")) | |
.Subscribe(keystrokeList => keystrokeBuffer.OnNext(keystrokeList)); | |
// Redo the last keystrokes and clear the buffer when the button to redo keystrokes is pressed | |
keystrokeBuffer.Do(keystrokeList => | |
{ | |
foreach (string keystroke in keystrokeList) | |
{ | |
Console.Write(keystroke); | |
} | |
Console.WriteLine(); | |
keystrokeBuffer.OnNext(new List<string>()); | |
}).Subscribe(); | |
// Clear the buffer when the button to clear the buffer is pressed | |
keystrokes.Where(key => key == "C") | |
.Subscribe(_ => keystrokeBuffer.OnNext(new List<string>())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
by chatgpt