Skip to main content

VirtualKeyboard

Accelerate

The VirtualKeyboard is a standalone control that provides an on-screen keyboard that can be manually placed in your application's layout. Unlike VirtualKeyboardScope, which automatically manages keyboard visibility based on focus, VirtualKeyboard is explicitly directed at a specific target input element.

Overview

VirtualKeyboard gives you more control over keyboard placement and behavior. It sends input directly to its designated target regardless of which control has input focus. This makes it useful for specialized input scenarios where automatic focus-based keyboard display isn't appropriate.

Properties

PropertyTypeDescription
TargetIInputElementGets or sets the input element that will receive keystrokes from the keyboard.
InputMethodsIEnumerable<VirtualKeyboardInputMethod>Gets or sets the collection of input methods (keyboard layouts and IMEs) available to users.

Usage Examples

Basic Usage

<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox x:Name="InputField" />
<VirtualKeyboard Target="{Binding ElementName=InputField}"
InputMethods="en-US:kbd:standard" />
</StackPanel>
</Window>

Multiple Input Methods

<StackPanel>
<TextBox x:Name="EmailField" Watermark="Email address" />
<VirtualKeyboard Target="{Binding ElementName=EmailField}"
InputMethods="en-US:kbd:standard, de:kbd:standard, ja:ime:kana" />
</StackPanel>

Code-Behind Configuration

// Get input methods for specific languages using SelectMany + ToList
var inputMethods = new[] { "en-US", "ja", "de" }
.SelectMany(VirtualKeyboardInputMethod.GetInputMethodsForLanguage)
.ToList();

// Create and configure a VirtualKeyboard
var keyboard = new VirtualKeyboard
{
Target = myTextBox,
InputMethods = inputMethods
};

// Add it to the visual tree
myContainer.Children.Add(keyboard);

Working with TextInputOptions

TextInputOptions attached properties can be applied to the target element to customize keyboard behavior:

<StackPanel>
<TextBox x:Name="EmailField"
TextInputOptions.ContentType="Email"
TextInputOptions.ReturnKeyType="Next" />

<VirtualKeyboard Target="{Binding ElementName=EmailField}"
InputMethods="en-US:kbd:standard" />
</StackPanel>

When to Use VirtualKeyboard vs. VirtualKeyboardScope

Choose VirtualKeyboard when:

  • Fixed Target: You need the keyboard to always target a specific input control, regardless of focus.
  • Specialized Input: You're building a custom input experience where focus doesn't drive the keyboard target.

Choose VirtualKeyboardScope when:

  • Standard Input: You want the keyboard to follow focus automatically.
  • Simpler Integration: You prefer a container-based approach with fewer configuration options.
  • Automatic Visibility: You want automatic show/hide behavior based on focus changes.

Best Practices

  1. Set a Valid Target:

    • Always set the Target property to a valid input element that can receive keystrokes.
    • Without a valid target, keyboard input will have nowhere to go.
  2. Keyboard Placement:

    • Position the keyboard where it won't obscure important content, typically at the bottom of the screen.
    • Unlike VirtualKeyboardScope, VirtualKeyboard doesn't automatically manage content scrolling.
  3. Input Methods Selection:

    • Choose input methods appropriate for your target audience.
    • For international applications, include layouts for all supported regions.
  4. Memory Management:

    • If creating keyboards dynamically, remember to remove them from the visual tree when no longer needed.
  5. Responsive Design:

    • Plan your layout to accommodate the keyboard's space requirements.
    • Consider using a Grid with row definitions to allocate space for the keyboard.

Styling

The VirtualKeyboard control in Avalonia supports custom styling via named resources. You can override these resources in your application to customize the appearance of keyboard elements.

Customizable Resources

Below is a list of key resources you can override in your theme or resource dictionary:

KeyTypeDefaultNotes
KeyboardActionButtonBackgroundBrushGoldenrod
KeyboardActionButtonBackgroundPressedBrushPaleGoldenrod
KeyboardButtonBackgroundBrushGhostWhite
KeyboardButtonBackgroundPressedBrushFloralWhite
KeyboardButtonBorderBrushBrushBlack
KeyboardButtonFontSizeDouble24introduced in 11.3.1
KeyboardButtonForegroundBrushBlack
KeyboardFunctionalButtonBackgroundBrushLightSteelBlue
KeyboardFunctionalButtonBackgroundPressedBrushLightBlue
KeyboardPaneBackgroundBrushDarkGray
KeyboardPanePaddingThickness4
KeyboardPopupKeySelectedBackgroundBrushPaleTurquoise

How to Override

To customize, define these resources in your application theme or resource dictionary. For example:

<SolidColorBrush x:Key="KeyboardButtonForeground" Color="#FF0000" />

###ß Example: Custom Theme

<ResourceDictionary>
<SolidColorBrush x:Key="KeyboardPaneBackground" Color="#FFD700" />
<system:Double x:Key="KeyboardButtonFontSize">36</system:Double>
<!-- Add more overrides as needed -->
</ResourceDictionary>

Input methods

View supported input methods
IdentifierDescription
af:kbd:standardAfrikaans
ar:kbd:standardArabic
hy-AM:kbd:standardArmenian (Armenia) Phonetic
az-AZ:kbd:standardAzerbaijani (Azerbaijan)
eu-ES:kbd:standardBasque (Spain)
be-BY:kbd:standardBelarusian (Belarus)
bn-BD:kbd:standardBengali (Bangladesh)
bn-IN:kbd:standardBengali (India)
bg:kbd:standardBulgarian
bg:kbd:bdsBulgarian (BDS)
ca:kbd:standardCatalan
hr:kbd:standardCroatian
cs:kbd:standardCzech
da:kbd:standardDanish
nl:kbd:standardDutch
nl-BE:kbd:standardDutch (Belgium)
en-GB:kbd:standardEnglish (Great Britain)
en-IN:kbd:standardEnglish (India)
en-US:kbd:standardEnglish (United States)
eo:kbd:standardEsperanto
et-EE:kbd:standardEstonian (Estonia)
fi:kbd:standardFinnish
fr:kbd:standardFrench
fr-CA:kbd:standardFrench (Canada)
fr-CH:kbd:standardFrench (Switzerland)
gl-ES:kbd:standardGalician (Spain)
ka-GE:kbd:standardGeorgian (Georgia)
de:kbd:standardGerman
de-CH:kbd:standardGerman (Switzerland)
el:kbd:standardGreek
hi:kbd:standardHindi
hi:kbd:compactHindi (Compact)
hu:kbd:standardHungarian
is:kbd:standardIcelandic
it:kbd:standardItalian
it-CH:kbd:standardItalian (Switzerland)
ja:ime:kanaJapanese (Kana)
kn-IN:kbd:standardKannada (India)
kk:kbd:standardKazakh
km-KH:kbd:standardKhmer (Cambodia)
ky:kbd:standardKyrgyz
lo-LA:kbd:standardLao (Laos)
lv:kbd:standardLatvian
lt:kbd:standardLithuanian
mk:kbd:standardMacedonian
ml-IN:kbd:standardMalayalam (India)
mr-IN:kbd:standardMarathi (India)
mn-MN:kbd:standardMongolian (Mongolia)
ne-NP:kbd:romanizedNepali (Romanized)
ne-NP:kbd:traditionalNepali (Traditional)
nb:kbd:standardNorwegian Bokmål
fa:kbd:standardPersian
pl:kbd:standardPolish
pt-BR:kbd:standardPortuguese (Brazil)
pt-PT:kbd:standardPortuguese (Portugal)
ro:kbd:standardRomanian
ru:kbd:standardRussian
sr-Cyrl:kbd:standardSerbian (Cyrillic)
sr-Latn:kbd:standardSerbian (Latin)
sk:kbd:standardSlovak
sl:kbd:standardSlovenian
es:kbd:standardSpanish
es-419:kbd:standardSpanish (Latin America)
es-US:kbd:standardSpanish (United States)
sw:kbd:standardSwahili
sv:kbd:standardSwedish
tl:kbd:standardTagalog
ta-IN:kbd:standardTamil (India)
ta-SG:kbd:standardTamil (Singapore)
te-IN:kbd:standardTelugu (India)
th:kbd:standardThai
tr:kbd:standardTurkish
uk:kbd:standardUkrainian
uz-UZ:kbd:standardUzbek (Uzbekistan)
vi:kbd:standardVietnamese
zu:kbd:standardZulu

See also

Discussion

Have questions or feedback? Join the conversation below.