Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Add two new APIs for currently active keyboard layout
Browse files Browse the repository at this point in the history
  • Loading branch information
RaymondLim committed Oct 23, 2014
1 parent 7988c34 commit b99ffa7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
22 changes: 22 additions & 0 deletions appshell/appshell_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,28 @@ if (!appshell.app) {
configurable : false
});

/**
* Return the user's keyboard layout language.
*/
native function GetCurrentKeyboardLayout();
Object.defineProperty(appshell.app, "keyboard", {
writeable: false,
get : function() { return GetCurrentKeyboardLayout(); },
enumerable : true,
configurable : false
});

/**
* Return the user's keyboard layout device type.
*/
native function GetKeyboardType();
Object.defineProperty(appshell.app, "keyboardType", {
writeable: false,
get : function() { return GetKeyboardType(); },
enumerable : true,
configurable : false
});

/**
* Returns the full path of the application support directory.
* On the Mac, it's /Users/<user>/Library/Application Support[/GROUP_NAME]/APP_NAME
Expand Down
4 changes: 4 additions & 0 deletions appshell/client_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class AppShellExtensionHandler : public CefV8Handler {
retval = CefV8Value::CreateDouble(client_app_->GetElapsedMilliseconds());
} else if (name == "GetCurrentLanguage") {
retval = CefV8Value::CreateString(client_app_->GetCurrentLanguage());
} else if (name == "GetCurrentKeyboardLayout") {
retval = CefV8Value::CreateString(client_app_->GetCurrentKeyboardLayout());
} else if (name == "GetKeyboardType") {
retval = CefV8Value::CreateString(client_app_->GetKeyboardType());
} else if (name == "GetApplicationSupportDirectory") {
retval = CefV8Value::CreateString(ClientApp::AppGetSupportDirectory());
} else if (name == "GetUserDocumentsDirectory") {
Expand Down
2 changes: 2 additions & 0 deletions appshell/client_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class ClientApp : public CefApp,
// Platform-specific methods implemented in client_app_mac/client_app_win
double GetElapsedMilliseconds();
CefString GetCurrentLanguage();
CefString GetCurrentKeyboardLayout();
CefString GetKeyboardType();
std::string GetExtensionJSSource();
static CefString AppGetSupportDirectory();
static CefString AppGetDocumentsDirectory();
Expand Down
13 changes: 13 additions & 0 deletions appshell/client_app_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ CefString ClientApp::GetCurrentLanguage()
return CefString(loc);
}

CefString ClientApp::GetCurrentKeyboardLayout()
{
// TODO: Get the user's active keyboard layout language
// Returning the UI language for now
return GetCurrentLanguage();
}

CefString ClientApp::GetKeyboardType()
{
// This API may not be needed for Linux. So just return an empty string for now.
return CefString("");
}

std::string ClientApp::GetExtensionJSSource()
{
//# We objcopy the appshell/appshell_extensions.js file, and link it directly into the binary.
Expand Down
13 changes: 13 additions & 0 deletions appshell/client_app_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@
return result;
}

CefString ClientApp::GetCurrentKeyboardLayout()
{
// TODO: Get the user's active keyboard layout language
// Returning the UI language for now
return GetCurrentLanguage();
}

CefString ClientApp::GetKeyboardType()
{
// This API is used for Windows only. So just return an empty string on Mac.
return CefString(@"");
}

std::string ClientApp::GetExtensionJSSource()
{
std::string result;
Expand Down
36 changes: 31 additions & 5 deletions appshell/client_app_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@
#include <MMSystem.h>
#include <ShlObj.h>
#include <string>
#include <stdio.h>

extern DWORD g_appStartupTime;

CefString ClientApp::GetCurrentLanguage()
CefString GetLanguageFromId(LANGID langID)
{
// Get the user's selected language
// Defaults to the system installed language if not using MUI.
LANGID langID = GetUserDefaultUILanguage();

// Convert LANGID to a RFC 4646 language tag (per navigator.language)
int langSize = GetLocaleInfo(langID, LOCALE_SISO639LANGNAME, NULL, 0);
int countrySize = GetLocaleInfo(langID, LOCALE_SISO3166CTRYNAME, NULL, 0);
Expand All @@ -59,6 +56,35 @@ CefString ClientApp::GetCurrentLanguage()
return CefString(locale);
}

CefString ClientApp::GetCurrentLanguage()
{
// Get the user's selected language
// Defaults to the system installed language if not using MUI.
LANGID langID = GetUserDefaultUILanguage();
return GetLanguageFromId(langID);
}


CefString ClientApp::GetCurrentKeyboardLayout()
{
// Get the user's active keyboard layout language
int kbd = (int)GetKeyboardLayout(0);
LANGID langID = MAKELANGID(kbd & 0xFFFF, kbd & 0xFFFF0000);
return GetLanguageFromId(langID);
}

CefString ClientApp::GetKeyboardType()
{
// Get the user's active keyboard device type
int keyboardLayout = ((int)GetKeyboardLayout(0) & 0xFFFF0000) >> 16;
wchar_t *type = new wchar_t[10];
int len = swprintf_s(type, 10, L"%d", keyboardLayout);
std::wstring keyboardType(type);

delete [] type;
return CefString(keyboardType);
}

std::string ClientApp::GetExtensionJSSource()
{
extern HINSTANCE hInst;
Expand Down

0 comments on commit b99ffa7

Please sign in to comment.