Greetings folks!
I have updated the Clipboard Functions in order to read/write from non ASCII languages aswell.
Also the Visual Studio cpp included aswell, so you can develop it further!
There are basically two functions:
EU.SetClipboard("Встре́ча с медве́дем мо́жет быть о́чень опа́сна.")
EU.GetClipboard()
As an extra also a fine EncodeURI function in order to create some valid HTTP requests.
SourceText$ = EU.EncodeURI(SourceText$)
As long as the font allows it, you can now copy/ paste any character.
Example:
#import_plugin EU
// Project: EU
// Created: 2021-01-13
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "EU" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
SetPrintSize(18)
do
clipboard$ = GetClipboardText()
if GetRawKeyPressed(13)
EU.SetClipboard("Встре́ча с медве́дем мо́жет быть о́чень опа́сна.")
endif
print("New Utf-8 Clipboard")
print("Press enter to set clipboard text")
print("")
print("AGK Native Clipboard: "+clipboard$)
print("Plugin Clipboard: "+EU.GetClipboard()) // works
print("")
print("")
Print( ScreenFPS() )
Sync()
loop
Source Code Cpp:
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdio.h>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <string>
#include <locale>
#include <codecvt>
#include "../AGKLibraryCommands.h"
using namespace std;
//string wstring converter setup
using convert_type = codecvt_utf8<wchar_t>;
wstring_convert<convert_type, wchar_t> converter;
// encode url function
string url_encode(const string& value) {
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char)c);
escaped << nouppercase;
}
return escaped.str();
}
// wstring <> string converter helpers
wstring s2ws(const string& str)
{
using convert_typeX = codecvt_utf8<wchar_t>;
wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.from_bytes(str);
}
string ws2s(const wstring& wstr)
{
using convert_typeX = codecvt_utf8<wchar_t>;
wstring_convert<convert_typeX, wchar_t> converterX;
return converterX.to_bytes(wstr);
}
void toClipboard(wstring s)
{
const wchar_t* text = s.c_str();
int len = wcslen(text);
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(wchar_t)); wchar_t* buffer = (wchar_t*)GlobalLock(hMem);
memcpy(GlobalLock(hMem), s.c_str(), s.size() + 1);
GlobalUnlock(hMem);
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
}
wstring getClipboardTT() {
if (IsClipboardFormatAvailable(CF_UNICODETEXT))
{
if (OpenClipboard(NULL)) {
HANDLE clip = GetClipboardData(CF_UNICODETEXT);
if (clip != NULL)
{
WCHAR* c;
c = (WCHAR*)clip;
CloseClipboard();
return (WCHAR*)clip;
}
CloseClipboard();
return L"";
}
}
return L"";
}
// AGK connection
// ==============
// creates encoded url, usful for correct http requests
DLL_EXPORT char* EncodeURI(const char* str)
{
string complete;
complete = url_encode(str);
int length = complete.length() + 1;
char* str2 = agk::CreateString(length);
strcpy_s(str2, length, complete.c_str());
return str2;
}
DLL_EXPORT void SetClipboard(const char* str)
{
int size = ::MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
if (size < 0) {
return;
}
if (::OpenClipboard(NULL)) {
::EmptyClipboard();
HGLOBAL hGlobal = ::GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, (size + 1) * sizeof(WCHAR));
if (hGlobal != NULL) {
LPWSTR lpszData = (LPWSTR)::GlobalLock(hGlobal);
if (lpszData != nullptr) {
::MultiByteToWideChar(CP_UTF8, 0, str, -1, lpszData, size);
::GlobalUnlock(hGlobal);
::SetClipboardData(CF_UNICODETEXT, hGlobal);
}
}
::CloseClipboard();
}
// toClipboard( s2ws(str) );
}
DLL_EXPORT char* GetClipboard()
{
string complete;
complete = ws2s( getClipboardTT() );
int length = complete.length() + 1;
char* str2 = agk::CreateString(length);
strcpy_s(str2, length, complete.c_str());
return str2;
}
Hope this can help you out at some point.