KeyBindings.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. namespace Terminal.Gui.Input;
  2. /// <summary>
  3. /// Provides a collection of <see cref="Key"/>s bound to <see cref="Command"/>s.
  4. /// </summary>
  5. /// <seealso cref="KeyBindings"/>
  6. /// <seealso cref="KeyBindings"/>
  7. /// <seealso cref="Command"/>
  8. public class KeyBindings : InputBindings<Key, KeyBinding>
  9. {
  10. /// <summary>Initializes a new instance bound to <paramref name="target"/>.</summary>
  11. public KeyBindings (View? target) : base ((commands, key) => new (commands), new KeyEqualityComparer ())
  12. {
  13. Target = target;
  14. }
  15. /// <inheritdoc />
  16. public override bool IsValid (Key eventArgs) { return eventArgs.IsValid; }
  17. /// <summary>
  18. /// <para>
  19. /// Adds a new key combination that will trigger the commands in <paramref name="commands"/> on the View
  20. /// specified by <paramref name="target"/>.
  21. /// </para>
  22. /// <para>
  23. /// If the key is already bound to a different array of <see cref="Command"/>s it will be rebound
  24. /// <paramref name="commands"/>.
  25. /// </para>
  26. /// </summary>
  27. /// <remarks>
  28. /// </remarks>
  29. /// <param name="key">The key to check.</param>
  30. /// <param name="target">
  31. /// The View the commands will be invoked on. If <see langword="null"/>, the key will be bound to
  32. /// <see cref="Application"/>.
  33. /// </param>
  34. /// <param name="commands">
  35. /// The command to invoked on the <see paramref="target"/> when <paramref name="key"/> is pressed. When
  36. /// multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
  37. /// consumed if any took effect.
  38. /// </param>
  39. public void Add (Key key, View? target, params Command [] commands)
  40. {
  41. KeyBinding binding = new (commands, target);
  42. Add (key, binding);
  43. }
  44. /// <summary>
  45. /// The view that the <see cref="KeyBindings"/> are bound to.
  46. /// </summary>
  47. /// <remarks>
  48. /// If <see langword="null"/> the KeyBindings object is being used for Application.KeyBindings.
  49. /// </remarks>
  50. public View? Target { get; init; }
  51. }