Cant set keyboard layout to wanted - c++

I am writing a program and I need to change input language to english if it isn't.
I use LoadKeyboardLayout() and ActivateKeyboardLayout() but it doesen't change the input language. What am I doing wrong?
Code:
int Functions::KeyPush(char Index)
{
LoadKeyboardLayout("00000409", KLF_ACTIVATE);
ActivateKeyboardLayout(HKL_PREV, KLF_SETFORPROCESS);
keybd_event(
Index,
Index,
KEYEVENTF_EXTENDEDKEY | 0,
0);
keybd_event(
Index,
Index,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
return GetAsyncKeyState(Index);
}

Related

Is it possible to type in uppercase when using Virtual Key Codes?

Is there any function that can make Virtual Key Codes type in uppercase?
This is the code I'm using
int main(){
keybd_event(0x41, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(0x41, 0, KEYEVENTF_KEYUP, 0);
return 0;}

how to simulate Ctrl + V using keybd_event () in C++

i want to paste text from clipboard to some program's textbox. so i tried to use keybd_event.
keybd_event(VK_CONTROL,0x1D, 0, 0);
keybd_event('V', 0x2F, 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);
but this is not executed. so i tried different way
keybd_event(VK_CONTROL,0x1D, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event('V', 0x2F, 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);
so this work well. However, since then, all keyboard inputs have been entered with the ctrl key pressed.
maybe i think key-up message is not worked
how to solve this problem?
However, since then, all keyboard inputs have been entered with the
ctrl key pressed. maybe i think key-up message is not worked
You simulate right CONTROL WM_KEYDOWN but left CONTROL WM_KEYUP. So the right-hand CTRL key has not been released.
The following code will work:
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
extended-key flag (KEYEVENTF_EXTENDEDKEY)
Indicates whether the key is an extended key, such as the right-hand
ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard.
The value is 1 if it is an extended key; otherwise, it is 0.
Refer to WM_KEYUP message.
keybd_event function has been superseded. Use SendInput instead.

Programmaticaly simulating Alt + Enter key press is not working

Here is my code:
keybd_event(VK_MENU, 0, 0, 0);
keybd_event(VK_RETURN, 0, 0, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
The first line would press Alt
The second line would press Enter ↵ (or Return ↵),
The fourth line would release Alt,
The fifth line would release Enter ↵ (or Return ↵).
You are not setting the KEYEVENTF_EXTENDEDKEY flag to keep the keys pressed down. Change your code to:
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_EXTENDEDKEY, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
Also you really don't need the sleep in the middle if you are just sending a Alt + Enter
You can see all of the keycodes here at the MSDN page.
Alt = VK_MENU
Left Alt = VK_LMENU
Right Alt Gr = VK_RMENU

Get text in a listbox control added to a child window

I want to add the text in a listbox control to the child of my main window. The child is essentially an edit control, but is not a dialog. I have tried a few different functions already with no success, I believe my problem is that I need to somehow switch focus from the dialog window to the child window before adding the text. I would prefer not to get an answer with specific code, but if I could be pointed to a helpful function or concept, that would be great!
EDIT: The listbox is part of a larger dialog window that allows the user to enter text and then add it to the list. These functions are working very well. What I'd like to do is get the text that is added to the list moved into the child window when the user clicks a button on the dialog, preferably without the user having to select the items before clicking the button.
There's a lot of code, but I think these pieces are relevant:
Child window:
case WM_CREATE:
{
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 100, 100, w, (HMENU) IDC_EDIT, NULL, NULL);
if (hEdit == NULL){
MessageBox(NULL, "Could not create child window :(", "ERROR", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
}
break;
case WM_SIZE:
{
RECT wSize;
GetClientRect(w, &wSize);
SetWindowPos(hEdit, NULL, 0, 0, wSize.right, wSize.bottom, NULL);
}
Function to add text to child window by clicking a button on the dialog (HWND hEdit, the child window, is defined globally):
case ID_ADDMAIN:
{
HWND hList = GetDlgItem(w, IDC_LIST1);
int count = SendMessage(hList, LB_GETCOUNT, NULL, NULL);
if (count > 0){
DWORD textLength = GetWindowTextLength(hList);
LPSTR alloc;
alloc = (LPSTR) GlobalAlloc(GPTR, textLength + 1);
if(GetWindowText(hList, alloc, textLength + 1)){
SendMessage(hEdit, WM_SETTEXT, NULL, (LPARAM) alloc);
}
GlobalFree(alloc);
}
else{
MessageBox(NULL, "There's nothing to add!", "???", MB_ICONINFORMATION | MB_OK);
}
}
break;
Besides the SendMessage function, I also tried SetWindowText and I tried getting each string in the listbox separately using a for loop rather than GetWindowText. Thank you in advance for your help.
You misunderstand the list box. The LB_GETCOUNT message is used to get the number of items (rows) in the list box. (Not the number of characters.)
GetWindowText is not appropriate for a list box: It gets the titlebar text, but a list box has no titlebar. What you can do with a list box is find out which row is selected (LB_GETCURSEL) and then get the text from that row (LB_GETTEXT).
Here is a list of functions I wrote for my WINAPI class library..
int ListBox::GetItemCount() const
{
return SendMessage(Control::GetHandle(), LB_GETCOUNT, 0, 0);
}
int ListBox::GetSelectedIndex() const
{
return SendMessage(Control::GetHandle(), LB_GETCURSEL, 0, 0);
}
void ListBox::SetSelectedIndex(int Index)
{
SendMessage(Control::GetHandle(), LB_SETCURSEL, Index, 0);
}
void ListBox::AddItem(const tstring &Item, int Index)
{
SendMessage(Control::GetHandle(), Index == 0 ? LB_ADDSTRING : LB_INSERTSTRING, Index, reinterpret_cast<LPARAM>(Item.c_str()));
}
void ListBox::RemoveItem(int Index)
{
SendMessage(Control::GetHandle(), LB_DELETESTRING, Index, 0);
}
void ListBox::Clear()
{
SendMessage(Control::GetHandle(), LB_RESETCONTENT, 0, 0);
}
int ListBox::GetIndexOf(const tstring &Item)
{
return SendMessage(Control::GetHandle(), LB_FINDSTRINGEXACT, -1, reinterpret_cast<LPARAM>(Item.c_str()));
}
int ListBox::GetIndexPartial(const tstring &Item)
{
return SendMessage(Control::GetHandle(), LB_FINDSTRING, -1, reinterpret_cast<LPARAM>(Item.c_str()));
}
void ListBox::SetColumnWidth(int Width)
{
SendMessage(Control::GetHandle(), LB_SETCOLUMNWIDTH, Width, 0);
}
tstring ListBox::GetItem(int Index) const
{
std::vector<char> Buffer(SendMessage(Control::GetHandle(), LB_GETTEXTLEN, Index, 0) + 1);
SendMessage(Control::GetHandle(), LB_GETTEXT, Index, reinterpret_cast<LPARAM>(Buffer.data()));
return tstring(Buffer.begin(), Buffer.end());
}
You'll need:
ListBox::GetItem(ListBox::GetSelectedIndex());
to get the text of the current item in the list box..
For the Edit control, I have:
void TextBox::SetReadOnly(bool ReadOnly)
{
SendMessage(Control::GetHandle(), EM_SETREADONLY, ReadOnly, 0);
}
void TextBox::SetPassword(bool Enabled, char PasswordChar)
{
SendMessage(Control::GetHandle(), EM_SETPASSWORDCHAR, Enabled ? PasswordChar : 0, 0);
}
std::uint32_t TextBox::GetTextLength() const
{
return GetWindowTextLength(Control::GetHandle());
}
void TextBox::ShowScrollBar(bool Show, int wBar)
{
::ShowScrollBar(Control::GetHandle(), wBar, true);
}
void TextBox::AppendText(const tstring &Text) const
{
SendMessage(Control::GetHandle(), EM_SETSEL, -1, -1);
SendMessage(Control::GetHandle(), EM_REPLACESEL, 0, reinterpret_cast<LPARAM>(Text.c_str()));
}
tstring TextBox::GetText() const
{
std::vector<TCHAR> Buffer(GetWindowTextLength(Control::GetHandle()) + 1);
GetWindowText(Control::GetHandle(), Buffer.data(), Buffer.size());
return tstring(Buffer.begin(), Buffer.end());
}
void TextBox::SetText(const tstring &Text)
{
this->Title = Text;
SetWindowText(Handle, Text.c_str());
}
You can get the text from the edit box easily with these.. I hope these aid you greatly..

WinApi - change Window Style

I want to change my windo style during runtime. I use this code
if (this->fullscreen)
{
this->style = WS_POPUP|WS_VISIBLE;
}
else
{
this->style = WS_OVERLAPPED|WS_SYSMENU|WS_VISIBLE;
}
SetWindowLongPtr(this->mainWindowHandle, GWL_STYLE, this->style);
SetWindowPos(this->mainWindowHandle,
HWND_TOP,
0,
0,
0, //New Width
0, //New Height,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
But it has no effect... and window is still without border (WS_POPUP)...
According to MSDN, you can't modify those particular styles after the window is created. If you're going to try to anyway, it also says that WS_SYSMENU requires WS_CAPTION.
Try calling SetWindowPos with the flag SWP_DRAWFRAME and see if it helps.
You might need to use CWnd::ModifyStyle. Have a look at example here
You might save the current pos and size from the actual window. Then destroy it an create an new window with the new style, previous pos and size.