<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Joan Magnet</title>
    <description>The latest articles on Forem by Joan Magnet (@mcnets).</description>
    <link>https://forem.com/mcnets</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1965384%2F8310d78d-4af2-46e4-8dca-ca03ab84c1ec.png</url>
      <title>Forem: Joan Magnet</title>
      <link>https://forem.com/mcnets</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mcnets"/>
    <language>en</language>
    <item>
      <title>UnoKeyboard</title>
      <dc:creator>Joan Magnet</dc:creator>
      <pubDate>Thu, 22 Aug 2024 16:48:38 +0000</pubDate>
      <link>https://forem.com/uno-platform/unokeyboard-23po</link>
      <guid>https://forem.com/uno-platform/unokeyboard-23po</guid>
      <description>&lt;p&gt;UnoKeyboard is an on-screen keyboard control designed to run on Desktop, WASM, and Windows platforms. It's primarily intended for touch-screen devices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cross-platform&lt;/li&gt;
&lt;li&gt;Customizable design&lt;/li&gt;
&lt;li&gt;Theming support&lt;/li&gt;
&lt;li&gt;Custom appearance&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adding the Control to Your Project
&lt;/h2&gt;

&lt;p&gt;The control is available as a &lt;a href="https://www.nuget.org/packages/UnoKeyboard" rel="noopener noreferrer"&gt;NuGet package&lt;/a&gt; Nuget package or can be integrated from the &lt;a href="https://github.com/mcNets/UnoKeyboard" rel="noopener noreferrer"&gt;GitHub source code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How should I use the Control?
&lt;/h2&gt;

&lt;p&gt;The keyboard can be used in two different ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using the AddKeyboard extension method&lt;/li&gt;
&lt;li&gt;Using a XAML control&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your project uses the default frame navigation, I would recommend using the AddKeyboard extension method. This method automatically shows and hides the keyboard when a TextBox gains or loses focus. On the other hand, if you prefer more control over the keyboard or if you are using other navigation methods, use the XAML control instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the AddKeyboard Extension Method
&lt;/h2&gt;

&lt;p&gt;The library provides an extension method for the Window class to automatically add the control to your project.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;AddKeyboard&lt;/code&gt; method injects a two-row grid. The first row contains a &lt;code&gt;ScrollViewer&lt;/code&gt;, and the second row displays the virtual keyboard. The content of the &lt;code&gt;ScrollViewer&lt;/code&gt;is assigned to the &lt;code&gt;RootFrame&lt;/code&gt;property of the &lt;code&gt;McWindowEx&lt;/code&gt;class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a reference to &lt;code&gt;McWindowEx.RootFrame&lt;/code&gt; in your &lt;code&gt;App.xaml.cs&lt;/code&gt; file:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public static Frame RootFrame =&amp;gt; McWindowEx.RootFrame;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Comment out the code that creates the main Frame in the OnLaunched method:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Do not repeat app initialization when the Window already has content,
    // just ensure that the window is active
    //if (MainWindow.Content is not Frame rootFrame)
    //{
    //    // Create a Frame to act as the navigation context and navigate to the first page
    //    rootFrame = new Frame();

    //    // Place the frame in the current Window
    //    MainWindow.Content = rootFrame;
    //    rootFrame.NavigationFailed += OnNavigationFailed;
    //}

    //if (rootFrame.Content == null)
    //{
    //    // When the navigation stack isn't restored navigate to the first page,
    //    // configuring the new page by passing required information as a navigation
    //    // parameter
    //    rootFrame.Navigate(typeof(MainPage), args.Arguments);
    //}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Call the &lt;code&gt;AddKeyboard&lt;/code&gt;method and navigate to the main page:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // Add UnoKeyboard to the Window
    MainWindow.AddKeyboard(height: 300);

    // Navigate using McWindowEx.RootFrame
    if (RootFrame.Content == null)
    {
        RootFrame.Navigate(typeof(MainPage), args.Arguments);
        RootFrame.NavigationFailed += OnNavigationFailed;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this point on, the virtual keyboard will automatically appear whenever a &lt;code&gt;TextBox&lt;/code&gt;gains focus.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using an XAML Control:
&lt;/h2&gt;

&lt;p&gt;Add a reference to the xmlns:ukc="using:UnoKeyboard.Controls" namespace and then add a new control to your file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ukc:UnoKeyboard x:Name="MyKeyboard"
                 Height="300"
                 Visibility="Collapsed"
                 HandleFocusManager="True" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Properties
&lt;/h2&gt;

&lt;p&gt;Here are some of the properties. For a complete list, refer to the control's documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Height
&lt;/h2&gt;

&lt;p&gt;This property defines the height of the virtual keyboard. It's important to note that the height of each key depends on the keyboard's height. For example, if the keyboard is 300px high and has 4 rows, each row will be::&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(300 - (Padding.Top + Padding.Bottom)) / 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, the width of each key is calculated based on the number of keys per row.&lt;/p&gt;

&lt;h2&gt;
  
  
  HandleFocusManager
&lt;/h2&gt;

&lt;p&gt;If the &lt;code&gt;HandleFocusManager&lt;/code&gt;property is set to True, the control will automatically show and hide the virtual keyboard when a TextBox gains or loses focus. Otherwise, the keyboard must be shown and hidden manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keyboard Type
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;McWindowEx&lt;/code&gt;extension class introduces a new attached property: &lt;code&gt;KeyboardType&lt;/code&gt;, which allows for keyboard selection. Two default keyboards are available, but you can add more custom keyboards:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;en-alfa&lt;/li&gt;
&lt;li&gt;numeric&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To use a specific keyboard, set the &lt;code&gt;KeyboardType&lt;/code&gt;attached property on your &lt;code&gt;TextBox&lt;/code&gt;control. The default keyboard is &lt;code&gt;en-alfa&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Page 
    xmlns:mck="using:UnoKeyboard" /&amp;gt;

&amp;lt;TextBox Width="200"
         VerticalAlignment="Center"
         FontSize="30"
         mck:McWindowEx.KeyboardType="numeric" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Customization
&lt;/h2&gt;

&lt;p&gt;Two static dictionaries are used to define the keyboard and its keys. You can add more keys and keyboard layouts by adding new entries to these dictionaries.&lt;/p&gt;

&lt;h2&gt;
  
  
  VirtualKeys
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/mcNets/UnoKeyboard/blob/main/src/UnoKeyboard/VirtualKeys.cs" rel="noopener noreferrer"&gt;The VirtualKeys.Key&lt;/a&gt; dictionary dictionary defines the keys that will be displayed on the keyboard. Each key is defined by a &lt;a href="https://github.com/mcNets/UnoKeyboard/blob/main/src/UnoKeyboard/Models/VirtualKeyModel.cs" rel="noopener noreferrer"&gt;VirtualKeyModel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That is a reduced version of the dictionary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static class VirtualKeys
{
    public static Dictionary&amp;lt;string, VirtualKeyModel&amp;gt; Key = new()
    {
        { "N1", new VirtualKeyModel("N1", KeyType.Text, "1", "1", 0x0031, 0x0031, null) },
        { "N2", new VirtualKeyModel("N2", KeyType.Text, "2", "2", 0x0032, 0x0032, null) },
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, to add the | key to your custom keyboard layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;VirtualKeys.Key.Add("|",                // Dictionary key
    new VirtualKeyModel("|",            // Key ID
                        KeyType.Text,   // Type
                        "|",            // Uppercase
                        "|",            // Lowercase
                        0x007C,         // Unicode uppercase
                        0x007C,         // Unicode lowercase
                        null));         // A Func&amp;lt;Microsoft.UI.Xaml.Shapes.Path&amp;gt;? that returns a Path
                                        // used to draw special keys.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Keyboards
&lt;/h2&gt;

&lt;p&gt;The Keyboards.Keyboard dictionary defines the keyboard layouts. Each keyboard is defined by a KeyboardModel.&lt;/p&gt;

&lt;p&gt;Let's add the new key to the keyboard layout:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Keyboards.Keyboard.Add("my_keyboard",   // Dictionary key
    new KeyboardModel("my_keyboard"     // Keyboard Id
                      "1",              // Number of pages.
                      "3",              // Number of rows.
                      "10",             // Max. keys per row.
                      [
                        new KeyModel(0,                     // Page 0
                                     0,                     // Row 0
                                     1,                     // Column 1
                                     1,                     // Column span
                                     VirtualKeys.Get("|")), // Key
                      ]));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For any questions or suggestions, feel free to contact me in the &lt;a href="https://github.com/mcNets/UnoKeyboard/discussions" rel="noopener noreferrer"&gt;Discussions&lt;/a&gt; section or &lt;a href="https://github.com/mcNets/UnoKeyboard/issues" rel="noopener noreferrer"&gt;open an Issue&lt;/a&gt; on the GitHub repository. You can also find me active in the &lt;a href="https://platform.uno/discord" rel="noopener noreferrer"&gt;Uno Platform Discord server&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Everyone is welcome to contribute to the project. Thank you for your interest!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Joan Magnet Sabata&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>unoplatform</category>
      <category>csharp</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
