Reputation: 1975
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?
Upvotes: 8
Views: 2720
Reputation: 1
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.
Upvotes: 0
Reputation: 125207
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.
Solution Explorer → Right click on your project → Choose Edit Project File.
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>
Tools → NuGet Package Manager → Package Manager Settings → Make sure PackageReference is selected for Default package management format.
Solution Explorer → Right click on your project → choose Manage NuGet Packages.
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.Upvotes: 6