Skip to content
Permalink
Browse files

Added C# example for NVDA controller client DLL (PR #9600)

C# example containing a PInvoke wrapper around nvdaControllerClient.dll and demonstration of how to use it.
The example speaks a message and outputs a different braille message. It does this 4 times, each time is has a short time delay.
  • Loading branch information...
DataTriny authored and feerrenrut committed May 21, 2019
1 parent aa280b6 commit e5a362dffd0534b2a9a745247c2313e056d26e23
Showing with 93 additions and 1 deletion.
  1. +3 −1 extras/controllerClient/readme.html
  2. +90 −0 extras/controllerClient/x86/example_csharp.cs
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html>
<head>
<title>NVDA Controller Client API 1.0 Documentation</title>
@@ -27,11 +28,12 @@ <h2>What is Included?</h2>
<li>nvdaController.h: a C header file containing declarations for all the provided functions.</li>
</ul>
<p>
The x86 directory also contains two example files:
The x86 directory also contains three example files:
</p>
<ul>
<li>example_python.py: an example Python program that uses the NVDA controller client API.</li>
<li>example_c.c: The source code for an example C program that uses the NVDA controller client API.</li>
<li>example_csharp.cs: The source code for an example C# program that uses the NVDA controller client API.</li>
</ul>
<h2>Available Functions</h2>
<p>
@@ -0,0 +1,90 @@
// Copyright(C) 2017-2019 NV Access Limited, Arnold Loubriat
// This file is covered by the GNU Lesser General Public License, version 2.1.
// See the file license.txt for more details.

using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace NVAccess
{
/// <summary>
/// C# wrapper class for the NVDA controller client DLL.
/// </summary>
static class NVDA
{
[DllImport("nvdaControllerClient32.dll", CharSet = CharSet.Unicode)]
private static extern int nvdaController_brailleMessage(string message);

[DllImport("nvdaControllerClient32.dll")]
private static extern int nvdaController_cancelSpeech();

[DllImport("nvdaControllerClient32.dll", CharSet = CharSet.Unicode)]
private static extern int nvdaController_speakText(string text);

[DllImport("nvdaControllerClient32.dll")]
private static extern int nvdaController_testIfRunning();

/// <summary>
/// Indicates whether NVDA is running.
/// </summary>
public static bool IsRunning => nvdaController_testIfRunning() == 0;

/// <summary>
/// Tells NVDA to braille a message.
/// </summary>
/// <param name="message">The message to braille.</param>
public static void Braille(string message)
{
nvdaController_brailleMessage(message);
}

/// <summary>
/// Tells NVDA to immediately stop speaking.
/// </summary>
public static void CancelSpeech()
{
nvdaController_cancelSpeech();
}

/// <summary>
/// Tells NVDA to speak some text.
/// </summary>
/// <param name="text">The text to speak.</param>
/// <param name="interrupt">If true, NVDA will immediately speak the text, interrupting whatever it was speaking before.</param>
public static void Speak(string text, bool interrupt = true)
{
if (interrupt)
{
CancelSpeech();
}
nvdaController_speakText(text);
}
}

class Program
{
static void Main(string[] args)
{
// Test if NVDA is running.
if (!NVDA.IsRunning)
{
Console.Error.WriteLine("Error communicating with NVDA.");
}
else
{
// Speak and braille some messages.
for (int i = 0; i < 4; i++)
{
NVDA.Speak("This is a test client for NVDA!");
NVDA.Braille("Time: " + 0.75 * i + " seconds.");
Task.Delay(625).Wait();
NVDA.CancelSpeech();
}
NVDA.Speak("This is a test client for NVDA!");
NVDA.Braille("Test completed!");
}
Console.ReadKey(true);
}
}
}

0 comments on commit e5a362d

Please sign in to comment.
You can’t perform that action at this time.