Pane.SendKeysAsync
Sends keys to the pane.
Source and package
- SendKeysAsync ( request : Send
Keys , cancellationToken : CancellationRequest Token ) Task -
Examples#
Send literal text and a named Enter key, wait for a private completion signal, then require a whole captured output line. All scopes are disposed after errors or cancellation. Use Linux or macOS with Git, tmux 3.2a or newer, .NET SDK 10.0.303, and the .NET 8 and 10 runtimes. In an empty directory, fetch the documented library revision:
Terminal window $ git clone https://github.com/libtmux/libtmux-dotnet.git libtmux-source && \git -C libtmux-source checkout --detach cf34b255c896d19a97e7d7fabea58609c916012dSave this complete consumer setup as global.json: View source
{"sdk": {"version": "10.0.303","rollForward": "disable","allowPrerelease": false},"test": {"runner": "Microsoft.Testing.Platform"}}Save this complete consumer setup as NuGet.config: View source
<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><clear /><add key="libtmux-source" value="libtmux-source/artifacts/api-example-packages" /><add key="nuget.org" value="https://api.nuget.org/v3/index.json" /></packageSources><packageSourceMapping><clear /><packageSource key="libtmux-source"><package pattern="LibTmux" /><package pattern="LibTmux.*" /></packageSource><packageSource key="nuget.org"><package pattern="*" /></packageSource></packageSourceMapping></configuration>Save this complete consumer setup as Example.csproj: View source
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFrameworks>net8.0;net10.0</TargetFrameworks><LangVersion>12.0</LangVersion><Nullable>enable</Nullable><ImplicitUsings>disable</ImplicitUsings><EnableDefaultCompileItems>false</EnableDefaultCompileItems><TreatWarningsAsErrors>true</TreatWarningsAsErrors><AnalysisLevel>10-recommended</AnalysisLevel></PropertyGroup><ItemGroup><Compile Include="Program.cs" /><PackageReference Include="LibTmux" Version="[0.0.0-alpha.20]" /></ItemGroup></Project>Save this complete program as Program.cs. It creates a private server; await using disposes its owned scopes after success, cancellation or failure. The complete file contains explicit imports, a top-level async entry point and a cancellation deadline. View source
using System;using System.Collections.Generic;using System.Linq;using System.Threading;using LibTmux;if (OperatingSystem.IsWindows()){throw new PlatformNotSupportedException("This example requires tmux on Linux or macOS.");}using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(10));CancellationToken token = deadline.Token;string binary = Environment.GetEnvironmentVariable("LIBTMUX_TMUX") ?? "tmux";string socketName = "csharp-inputcapture-" + Guid.NewGuid().ToString("N");ServerConnectionOptions options = new(){SocketName = socketName,ConfigurationFile = "/dev/null",TmuxBinaryPath = binary,};await using OwnedServerScope owned = await Server.CreateOwnedAsync(options, token);await using OwnedSessionScope session = await owned.Value.CreateOwnedSessionAsync(new NewSessionRequest { Name = "demo", Command = "/bin/sh" }, token);Server server = await Server.ConnectAsync(options, token);Pane pane = (await server.GetPanesAsync(token)).Single();string channel = "capture-" + Guid.NewGuid().ToString("N");await using TmuxWaitChannel wait = server.OpenWaitChannel(channel);const string marker = "api capture ready";string command = $"printf '%s\\n' {QuoteShell(marker)}; "+ $"{QuoteShell(binary)} -L {QuoteShell(socketName)} wait-for -S {QuoteShell(channel)}";await pane.SendKeysAsync(new SendKeysRequest { Text = command, Literal = true, Enter = false }, token);await pane.SendKeysAsync(new SendKeysRequest { Text = "Enter", Enter = false }, token);if (!await wait.WaitAsync(TimeSpan.FromSeconds(5), token)){throw new InvalidOperationException("The pane did not signal that its output was ready.");}IReadOnlyList<string> lines = await pane.CaptureAsync(new CapturePaneRequest { JoinWrappedLines = true }, token);// Joining wrapped lines preserves terminal padding after the output.if (!lines.Any(line => string.Equals(line.TrimEnd(), marker, StringComparison.Ordinal))){throw new InvalidOperationException("Capture did not contain the complete output line.");}Console.WriteLine(marker);static string QuoteShell(string value) => "'" + value.Replace("'", "'\\''", StringComparison.Ordinal) + "'";Build the exact source package and the saved consumer, then run both supported frameworks. The local package feed prevents an unrelated published package from satisfying the restore. Each program checks its result and exits unsuccessfully after an unhandled error:
Terminal window $ export NUGET_PACKAGES="$PWD/packages" && \dotnet restore libtmux-source/src/LibTmux/LibTmux.csproj --locked-mode && \dotnet pack libtmux-source/src/LibTmux/LibTmux.csproj \--configuration Release --no-restore -p:ContinuousIntegrationBuild=true \--output "$PWD/libtmux-source/artifacts/api-example-packages" && \dotnet restore Example.csproj --configfile NuGet.config && \dotnet build Example.csproj --configuration Release --no-restore --warnaserror && \export LIBTMUX_TMUX="$(command -v tmux)" && \export TMUX_TMPDIR=/tmp/libtmux-dotnet-dev && \mkdir -p "$TMUX_TMPDIR" && \unset TMUX TMUX_PANE && \dotnet bin/Release/net8.0/Example.dll && \dotnet bin/Release/net10.0/Example.dllExpected program output from each framework:
api capture ready- Parameters
-
-
request ( Send
Keys ) – What to send.Request -
cancellationToken ( Cancellation
Token ) – Cancels the tmux commands.
-
- Raises
-
-
Argument
Exception – The request sends nothing or its text contains NUL. -
Lib
Tmux – Text was sent, but a requested Enter failed. Its dispatch state is unknown, so the whole request must not be retried.Exception
-
tmux command
send-keysDiscussed in Pane interaction
In other ports Send keystrokes to a pane
- Python
libtmux.Pane.send_keys - Ruby
LibTmux::Pane#send_keys - Lua
libtmux.Pane:send_keys - TypeScript
pane.Pane.sendKeys - Rust
pane.Pane.send_keys - Go
tmux.Pane.SendKeys - Java no direct equivalent;
Pane.pastewrites a buffer instead - C++
libtmux::Pane::send_text - Swift
Server.sendKeys(_:to:literally:) - Kotlin
io.github.libtmux.kotlin.Pane.sendKeys - Scala
io.github.libtmux.scaladsl.Pane.sendKeys - F# No source-verified F# equivalent is recorded for this operation.