wxwidgets accessing windows clipboard and key/key-combo hook question

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

wxwidgets accessing windows clipboard and key/key-combo hook question

Post by MagickPanda »

I am wonder if wxWidgets supports accessing/editing windows clipboard content, and if wxWidgets have 'global'(e.g:outside wxWidgets main window, in a browser or some other application) key pressed/released etc event detection capability.

I am planning writing an application using wxWidgets that reads stuff from a csv, whenever you press a key-combination, the application reads from csv certain field's contents, then copies it to the windows clipboard. It will a lot easier if wxWidgets supports such functions.

Thanks.
catalin
Moderator
Moderator
Posts: 1618
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: wxwidgets accessing windows clipboard and key/key-combo hook question

Post by catalin »

See wxClipboard class and wxWindow::RegisterHotKey() function in the docs.
MagickPanda
Experienced Solver
Experienced Solver
Posts: 81
Joined: Wed Oct 19, 2016 1:41 pm

Re: wxwidgets accessing windows clipboard and key/key-combo hook question

Post by MagickPanda »

catalin wrote:See wxClipboard class and wxWindow::RegisterHotKey() function in the docs.
Thanks for the tips, I will look at the docs a bit.
RP__
Earned some good credits
Earned some good credits
Posts: 124
Joined: Tue Jan 20, 2015 5:53 pm

Re: wxwidgets accessing windows clipboard and key/key-combo hook question

Post by RP__ »

You could indeed use those two classes, I use them too and they're really helpful.

Register the hotkey and make sure to unregister them as well.
Use the wxEVT_HOTKEY event to handle the combinations.
I remember that I used MSW MOD_SHIFT instead of the wxWidgets variant for some reason, but I am not sure whether that was because they wouldn't work otherwise. Something to keep in mind if they don't work out of the box.

This is the code I use to fill the clipboard:

Code: Select all

void INativeUser::insertTextToClipboard(const std::wstring& wsText) {
	if (wxTheClipboard->Open())
	{
		wxTheClipboard->Clear();
		wxTheClipboard->SetData(new wxTextDataObject(wsText));
		wxTheClipboard->Flush();
		wxTheClipboard->Close();
	}
	else {
		//log error (L"Failed to insert text into clipboard (closed): '" + wsText + L"'.", Log::FAILURE);
	}
}
And I use this to read the clipboard:

Code: Select all

std::wstring INativeUser::getClipboardText() {
	std::wstring ret;
	if (wxTheClipboard->Open())
	{
		if (wxTheClipboard->IsSupported(wxDF_UNICODETEXT))
		{
			wxTextDataObject data;
			wxTheClipboard->GetData(data);
			ret = data.GetText().ToStdWstring();
		}
		wxTheClipboard->Close();
	}
	return ret;
}
Post Reply