Programmatically Launching Windows 10 Emoji Hotkeys
Asked Answered
S

2

8

The latest major update for Windows 10, "Fall Creators Update" (AKA RedStone3), has added the functionality of a system-wide emoji pop-up that can be used in any textbox.

I'm trying to make a program that would launch that same pop-up emoji window when clicking on a button. As suggested in another discussion about similar topic, I've tried to use the InputSimulator project. There are also other ways, as suggest here, but seems like that simulator is wrapping them pretty well.

All I did was to create a new small WPF application, with one main window which has a TextBox and a button. Pressing the button would run the following code:

textBox.Focus()
new InputSimulator().Keyboard.ModifiedKeyStroke(VirtualKeyCode.LWIN, VirtualKeyCode.OEM_PERIOD);

This seems to have no affect! I've also tried OEM_1 (which is the ":;" keycode) instead of OEM_PERIOD, but still no luck. The thing is, any other combination of LWIN with another key (such as VK_P) would work with the same simulator's code.

If I try to press the Emoji Hotkeys on the real keyboard, after running that code, the first press does nothing (sometimes the emoji pop-up shows for half a second and disappears right after) and then need to press the hotkeys again in order for the popup to show. This makes me suspect that maybe the popup does show, only outside of the screen bounds, or perhaps waiting for another keyboard event to happen/finish?

Scutellation answered 26/10, 2017 at 19:23 Comment(3)
Note that the Emoji gimmick only works with a US keymap for some bizarre reason.Whipsaw
@JonathanPotter didn't notice that before, thanks! Though I've tried the said code on English keyboard, of course.Scutellation
Note that after the update to RS4 (April 2018), the Emoji hotkeys should now work in all languageScutellation
N
6

Open Emoji panel in a Windows Forms or WPF application

You need to handle the desired event, then first Focus to your control, then using CoreInputView.GetForCurrentView get the core input view for the current window, and then call its TryShow method and pass CoreInputViewKind.Emoji to the method. For example:

//using Windows.UI.ViewManagement.Core;
private async void button1_Click(object sender, EventArgs e)
{
    textBox1.Focus();
    CoreInputView.GetForCurrentView().TryShow(CoreInputViewKind.Emoji);
}

Note: For Windows Forms or WPF project, before using above code, you need to configure your project to be able to call Windows Runtime APIs in desktop apps.

Call Windows Runtime APIs in Windows Forms or WPF

.NET 5

  1. Solution Explorer → Right click on your project → Choose Edit Project File.

  2. Change the value of TargetFramework to one of the following strings and save changes.

    • net5.0-windows10.0.17763.0: for targeting Windows 10, version 1809.
    • net5.0-windows10.0.18362.0: for targeting Windows 10, version 1903.
    • net5.0-windows10.0.19041.0: for targeting Windows 10, version 2004.

    For example:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net5.0-windows10.0.18362.0</TargetFramework>
        <UseWindowsForms>true</UseWindowsForms>
      </PropertyGroup>
    </Project>
    

.NET 4.X

  1. Tools → NuGet Package Manager → Package Manager Settings → Make sure PackageReference is selected for Default package management format.

  2. Solution Explorer → Right click on your project → choose Manage NuGet Packages.

  3. Find Microsoft.Windows.SDK.Contracts package. In the right pane of the NuGet Package Manager window select the desired version of the package based on the version of Windows 10 you want to target and click install:

    • 10.0.19041.xxxx: for targeting Windows 10, version 2004.
    • 10.0.18362.xxxx: for targeting Windows 10, version 1903.
    • 10.0.17763.xxxx: for targeting Windows 10, version 1809.
    • 10.0.17134.xxxx: for targeting Windows 10, version 1803.
Nathalie answered 23/12, 2020 at 19:13 Comment(2)
Thank you very much for the detailed answer! I can't test it right now, but I'll accept it since it seems reliable :) BTW, I think that CoreInputViewKind.Emoji wasn't defined yet when this question was originally asked (3 years ago)Scutellation
It most likely is the case, the commit history of CoreInputViewKind shows the first useful commit at June 2018. Thanks for the feedback, I've tested both .NET 5 and .NET 4.8 myself before posting the answer.Nathalie
F
0

Since I couldn't get these more elegant solutions to work, I resorted to keyboard calls. This works just fine for my needs, so I thought I'd share.

 Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As IntPtr, ByVal bScan As IntPtr, ByVal dwFlags As IntPtr, ByVal dwExtraInfo As IntPtr)

 Private Sub EmojiLaunch_Click(sender As Object, e As EventArgs) Handles EmojiLaunch.Click

    Call keybd_event(&H5B, 0, &H0, 0) 'Windows Key Down
    Call keybd_event(&HBE, 0, &H0, 0) 'Period Key Down
    Call keybd_event(&HBE, 0, &H2, 0) 'Period Key Up
    Call keybd_event(&H5B, 0, &H2, 0) 'Windows Key Up

 End Sub

EmojiLaunch is a Label, you don't want to use a button since it changes the focus.

Foreshadow answered 13/2, 2021 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.