# CommandData

Kind: Class · Namespace: Sansar.Simulation · Assembly: Sansar.Simulation.dll · Script API 47.10.0

<div class="api-meta">
<span class="api-kind api-kind-class" data-pagefind-filter="kind:Class">Class</span>
<span data-pagefind-filter="namespace:Sansar.Simulation">Namespace <a href="/scripting/api/sansar.simulation"><code>Sansar.Simulation</code></a></span>
<span>Inherits <a href="/scripting/api/sansar.script/eventdata">Sansar.Script.EventData</a></span>
</div>

<span id="T:Sansar.Simulation.CommandData"></span>

Command events are generated by the client when input events occur.

```csharp
public class CommandData : Sansar.Script.EventData
```

## Remarks

| Command | Default Key Binding |
|---|---|
| Trigger | Left Mouse Button |
| PrimaryAction | F |
| SecondaryAction | R |
| Modifier | Shift |
| Action1 to Action0 | Number keys 1 to 0 |
| Confirm | Enter |
| Cancel | Escape |
| SelectLeft | Left arrow |
| SelectRight | Right arrow |
| SelectUp | Up arrow |
| SelectDown | Down arrow |
| Keypad0 to Keypad9 | Numberpad keys 0 to 9 |
| KeypadEnter | Numberpad Enter |



While the agent is seated, the movement controls also generate keypad commands for vehicle driving: W/A/S/D and the VR or gamepad left analog stick map to Keypad8 (forward), Keypad4 (left), Keypad6 (right), and Keypad2 plus Keypad5 (back/brake). These aliases are only delivered while seated; when standing, the movement controls move the avatar without generating commands. Stick axes generate digital press/release events at half deflection, not continuous analog values. Sources holding the same keypad command are combined: the first source sends Pressed and the last source to release sends Released, retaining the first source&#39;s ControlPoint.



An example script for handling client commands:

## Example

```csharp title="CommandExample.cs"
/* This content is licensed under the terms of the Creative Commons Attribution 4.0 International License.
 * When using this content, you must:
 * •    Acknowledge that the content is from the Sansar Knowledge Base.
 * •    Include our copyright notice: "2022 Sansar, Inc."
 * •    Indicate that the content is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
 * •    Include the URL for, or link to, the license summary at https://creativecommons.org/licenses/by-sa/4.0/deed.hi (and, if possible, to the complete license terms at https://creativecommons.org/licenses/by-sa/4.0/legalcode.
 * For example:
 * "This work uses content from the Sansar Knowledge Base. © 2022 Sansar, Inc." Licensed under the Creative Commons Attribution 4.0 International License (license summary available at https://creativecommons.org/licenses/by/4.0/ and complete license terms available at https://creativecommons.org/licenses/by/4.0/legalcode)."
 */

using Sansar.Script;
using Sansar.Simulation;

public class CommandExample : SceneObjectScript
{
    #region ScriptParameters
    [Tooltip(@"The command to enable listening for the Action Command. Default: Confirm (Enter)")]
    [DefaultValue("Confirm")]
    [DisplayName("Subscribe Command")]
    public readonly string SubscribeCommand;

    [Tooltip(@"The command to disable listening for the Action Command. Default: Cancel (Escape)")]
    [DefaultValue("Cancel")]
    [DisplayName("Unsubscribe Command")]
    public readonly string UnsubscribeCommand;

    [Tooltip(@"If the command has been subscribed to by the 'Subscribe Command', this will log the action to the script console.")]
    [DefaultValue("Trigger")]
    [DisplayName("Action Command")]
    public readonly string ActionCommand;
    #endregion ScriptParameters

    public override void Init()
    {
    // Subscribe to new user events;
    ScenePrivate.User.Subscribe(User.AddUser, NewUser);
    }

    IEventSubscription subscription = null;

    void NewUser(UserData newUser)
    {
        Client client = ScenePrivate.FindAgent(newUser.User).Client;

        // CommandReceived will be called every time the command it triggered on the client
        // CommandCanceled will be called if the subscription fails
        if (SubscribeCommand != "")
        {
            client.SubscribeToCommand(SubscribeCommand, CommandAction.Pressed, (data) =>
            {
                if (subscription == null)
                {
                    Log.Write(GetType().Name, $"[{SubscribeCommand}] Subscribing to {ActionCommand}.");
                    subscription = client.SubscribeToCommand(ActionCommand, CommandAction.All, CommandReceived, CommandCanceled);
                }
            }, CommandCanceled);
        }

        if (UnsubscribeCommand != "")
        {
            client.SubscribeToCommand(UnsubscribeCommand, CommandAction.Pressed, (data) =>
            {
                if (subscription != null)
                {
                    Log.Write(GetType().Name, $"[{UnsubscribeCommand}] Unsubscribing to {ActionCommand}.");
                    subscription.Unsubscribe();
                    subscription = null;
                }
            }, CommandCanceled);
        }
    }

    void CommandReceived(CommandData command)
    {
        Log.Write(GetType().Name, $"Received command {command.Command}: {command.Action}. Targeting Info: {command.TargetingComponent}, Origin:{command.TargetingOrigin}, Position:{command.TargetingPosition}, Normal{command.TargetingNormal}");
    }

    void CommandCanceled(CancelData data)
    {
        Log.Write(GetType().Name, "Subscription canceled: "+data.Message);
    }

}
```

<p class="api-example-source">Source: <code>Examples/CommandExample.cs</code> in the Sansar 47.10.0 client ScriptApi snapshot.</p>

## Members at a glance

| Kind | Members |
|---|---|
| **Properties** | [Action](#action) · [CameraControlMode](#cameracontrolmode) · [Command](#command) · [ControlPoint](#controlpoint) · [IsAimTarget](#isaimtarget) · [MouseLookMode](#mouselookmode) · [SessionId](#sessionid) · [SimulationFrame](#simulationframe) · [TargetingComponent](#targetingcomponent) · [TargetingNormal](#targetingnormal) · [TargetingOrigin](#targetingorigin) · [TargetingPosition](#targetingposition) |
| **Methods** | [ToString](#tostring) |

## Properties

### Action

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.Action"></span>

```csharp signature
public CommandAction Action { get; }
```

The action which occurred.

</div>

### CameraControlMode

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.CameraControlMode"></span>

```csharp signature
public CameraControlMode CameraControlMode { get; }
```



</div>

### Command

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.Command"></span>

```csharp signature
public string Command { get; }
```

The command which occurred.

</div>

### ControlPoint

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.ControlPoint"></span>

```csharp signature
public ControlPointType ControlPoint { get; }
```



</div>

### IsAimTarget

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.IsAimTarget"></span>
<div class="api-badges"><span class="api-badge api-since">Added in 37.0.0</span></div>

```csharp signature
public bool IsAimTarget { get; }
```



</div>

### MouseLookMode

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.MouseLookMode"></span>

```csharp signature
public bool MouseLookMode { get; }
```



</div>

### SessionId

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.SessionId"></span>

```csharp signature
public Sansar.Script.SessionId SessionId { get; }
```

The client which generated the event.

</div>

### SimulationFrame

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.SimulationFrame"></span>

```csharp signature
public ulong SimulationFrame { get; }
```



</div>

### TargetingComponent

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.TargetingComponent"></span>

```csharp signature
public Sansar.Script.ComponentId TargetingComponent { get; }
```



</div>

### TargetingNormal

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.TargetingNormal"></span>

```csharp signature
public Sansar.Vector TargetingNormal { get; }
```



</div>

### TargetingOrigin

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.TargetingOrigin"></span>

```csharp signature
public Sansar.Vector TargetingOrigin { get; }
```



</div>

### TargetingPosition

<div class="api-card">
<span id="P:Sansar.Simulation.CommandData.TargetingPosition"></span>

```csharp signature
public Sansar.Vector TargetingPosition { get; }
```



</div>


## Methods

### ToString

<div class="api-card">
<span id="M:Sansar.Simulation.CommandData.ToString"></span>
<span id="M:Sansar.Simulation.CommandData.ToString()"></span>

```csharp signature
public override string ToString()
```

A string representation of this object.

**Returns:** [string](https://learn.microsoft.com/dotnet/api/system.string) — A string representation of this object.

<details class="api-remarks"><summary>Remarks</summary>

The format of this string may change between releases.

</details>

</div>


## Used in examples

- [TeleportHotkeys.cs](/scripting/examples/teleport-hotkeys)
- [BanHammerExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/BanHammerExample.cs)
- [CommandExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/CommandExample.cs)
- [PewPewExample.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/PewPewExample.cs)
- [ScriptLibrary/Scene/Hotkey.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Hotkey.cs)
- [ScriptLibrary/Scene/Screenshot.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Screenshot.cs)
- [ScriptLibrary/Scene/Selector.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Selector.cs)
- [ScriptLibrary/Scene/Target.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Target.cs)
- [ScriptLibrary/Scene/Targeting.cs](https://github.com/Wookey-Technologies/sansar-script-starter/blob/66af8b516b701088d043e4615acfcf5779356604/examples/official/ScriptLibrary/Scene/Targeting.cs)

<p class="api-generated" data-pagefind-ignore>Generated from the Sansar 47.10.0 client ScriptApi snapshot.</p>