ARTICLE

Create Virtual Keyboard in Silverlight

Posted by Manish Tewatia Articles | Silverlight using VB.NET November 13, 2010
In this article you will see how to create an Virtual Keyboard in Silverlight.
Download Files:
 
Reader Level:

Virtual Keyboard: Virtual Keyboard is an On-Screen Keyboard in Silverlight with support for Copying the input to the Clipboard which can then be Pasted into another application. My implementation is easily customizable, and you can add as many keyboard layouts as you require, or even customize your own. There are numerous limitations as this is my first attempt to implement such a control.

Example of Virtual Keyboard

<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth
="648">
    <Grid x:Name="LayoutRoot" Background="AliceBlue" Height="329" Width="648">
        <Canvas Height="35" Width="643" VerticalAlignment="Top" HorizontalAlignment="Left" Name="box">
            <TextBox Canvas.Left="6" Canvas.Top="6" Height="23" Width="631" Name="store"/>
        </Canvas>
        <Canvas Height="292" Width="648" Margin="0,35,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Name="Content">
            <StackPanel Height="265" Width="643" Name="Keyboard"/>
        </Canvas>
    </Grid>
</
UserControl>

//MainPage.xaml.vb code

Private Sub Button_KeyPressed(ByVal Value As Object) _
        Handles Me.KeyPressed
        store.Focus()
        store.SelectedText = Value
    End Sub 
    Private Sub UpdateRow(ByRef Row As StackPanel, ByRef Source As String(,), _
               ByRef Shift As Boolean, ByRef Count As Integer)
        For Each Item As Button In Row.Children
            For Index As Integer = 0 To Count
                If Shift Then
                    If CStr(Item.Content) = CStr(Source(0, Index)) Then
                        Item.Content = Source(1, Index)
                    End If
                Else
                    If CStr(Item.Content) = CStr(Source(1, Index)) Then
                        Item.Content = Source(0, Index)
                    End If
                End If
            Next
        Next
    End Sub 
    Private Sub Button_Click(ByVal sender As System.Object, _
        ByVal e As System.Windows.RoutedEventArgs)
        Dim _value As New Object
        Dim _command As New Object
        _value = CType(sender, Button).Content
        _command = CType(sender, Button).Tag
        If Not _command Is Nothing Then
            _value = Nothing
            Select Case _command
                Case Sp.BackSpace, Sp.Copy, Sp.Paste, _
                    Sp.Home, Sp.End, Sp.Insert, Sp.Delete
                    RaiseEvent SpPressed(_command)
                Case Sp.Shift
                    _isSft = Not _isSft
                    UpdateRow(_panel1, _row1, _isSft, 12)
                    UpdateRow(_panel2, _row2, _isSft, 11)
                    UpdateRow(_panel3, _row3, _isSft, 11)
                    UpdateRow(_panel4, _row4, _isSft, 10)
                Case Sp.CapsLock
                    _isCps = Not _isCps
                Case Sp.Tab
                    _value = vbTab
                Case Sp.Enter
                    _value = vbCrLf
                Case Sp.Space
                    _value = " "
            End Select
        End If
        If Not _value Is Nothing Then
            If _isCps Xor _isSft Then
                _value = CStr(_value).ToUpper
            Else
                _value = CStr(_value).ToLower
            End If
            RaiseEvent KeyPressed(_value)
        End If
    End
Sub

Hear I will explain some events to read and learn the hole program please download the source code given at the top of article.

Output Window

1.gif

Conclusion

Hope this article would help you to understand how to create an Virtual Keyboard in Silverlight.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor