🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by nicolasr Tue, 08 Apr 2003 00:47
Sending VK_CONTROL/VK_HOME to a TStringGrid
#17160
Author: nicolasr
Date: Tue, 08 Apr 2003 00:47
20 lines
1586 bytes
Hi,

I need to select the first cell of a TStringGrid in another application. I
found that pressing CTRL-HOME does this. So I tried to send
VK_CONTROL/VK_HOME to the string grid in the other application but somehow
it looks as if only the HOME key is processed. (Pressing HOME moves the
selection rectangle to the first cell in a row)

Here is the code sequence I have tried (hwnd is the HWND of the
TStringGrid):

SendMessage(hwnd, WM_KEYDOWN, VK_CONTROL, 0);
SendMessage(hwnd, WM_KEYDOWN, VK_HOME, 0);
SendMessage(hwnd, WM_KEYUP, VK_HOME, 0);
SendMessage(hwnd, WM_KEYUP, VK_CONTROL, 0);

Anything wrong with this?

thanks for any idea,
Nick
Re: Sending VK_CONTROL/VK_HOME to a TStringGrid
#17162
Author: Remy Lebeau (TeamB)
Date: Tue, 08 Apr 2003 01:56
25 lines
1955 bytes
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net> wrote in message
news:3e921bff$1@newsgroups.borland.com...

> Anything wrong with this?

Yes - you can't send SHIFT, CTRL or ALT keypresses using messages for what
you're trying to do, especially since the VCL is invoved on the receiving
end.  The VCL doesn't get those values from any messages to begin with.  It
uses GetKeyState() instead.

You basically have two options that I can think of (both untested):

1) use GetWindowThreadProcessId(), AttachThreadInput(), GetKeyboardState()
and SetKeyboardState() to set the state of the VK_CONTROL key for the thread
that the TStringGrid is running in.  Then send the message for just VK_HOME
only, and then afterwards reset the keyboard state and detach from the
thread.

2) use a combination of GetWindow(), SetForegroundWindow() and SetFocus() to
make sure the TStringGrid has the input focus, then use keybd_event()
instead of sending messages.


Gambt
Re: Sending VK_CONTROL/VK_HOME to a TStringGrid
#17086
Author: nicolasr
Date: Tue, 08 Apr 2003 20:43
33 lines
1897 bytes
thanks for your reply!

I tried your first suggestion and it works well now. I post the code
below in case anyone is interested.

I have not tried the second way but I read many posts about issues
with SetForegroundWindow() under Win98 and up. The first way seems to
be more reliable.

thanks again,
Nick


//assuming hwnd is the window handle of a TStringGrid
//in another application
DWORD threadId = GetWindowThreadProcessId(hwnd, NULL);
DWORD ourThreadId = GetCurrentThreadId();

AttachThreadInput(threadId, ourThreadId, TRUE);

unsigned char keyboardState[256];
GetKeyboardState(keyboardState);
keyboardState[VK_CONTROL] |= 0x80;
SetKeyboardState(keyboardState);

SendMessage(hwnd, WM_KEYDOWN, VK_HOME, 0);
SendMessage(hwnd, WM_KEYUP, VK_HOME, 0);

GetKeyboardState(keyboardState);
keyboardState[VK_CONTROL] &= ~0x80;
SetKeyboardState(keyboardState);

AttachThreadInput(threadId, ourThreadId, FALSE);
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads