The MASM Forum

General => The Workshop => Windows API => Topic started by: NoCforMe on March 09, 2024, 03:14:24 PM

Title: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 09, 2024, 03:14:24 PM
Here's a problem for y'all:

Let's say a guy has a window which contains a read-only edit control that occupies the entire window. (The purpose of this window is to display the text of a file.) This part works fine.

Let's say further that this guy would like to be able to search text within the window. To do so he wants to be able to use the Ctrl-F (or F3) keystrokes to initiate the search operation.

Problem is that because the edit control is read-only, any keystrokes only cause the system to ding at the user, which is the expected behavior of a read-only edit control.

Is there any way for this window (either the containing window or its child edit control) to receive the keystroke notification (WM_CHAR or similar)? I naively tried using SetFocus() on the edit control, but that doesn't work.

I'm not going to post any code for this question. (My code is very complicated.) Let's keep this at a theoretical level for now.

And no, I'd rather not implement a menu for this window, even though that would solve the problem (by having a "Find" item in it). I'd rather use the hotkey approach, if it's possible.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: sinsi on March 09, 2024, 03:37:35 PM
About Keyboard Accelerators (https://learn.microsoft.com/en-us/windows/win32/menurc/about-keyboard-accelerators)
Or subclass the edit control?
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 09, 2024, 03:48:24 PM
Yes, I know about keyboard accelerators, use them in the surrounding code, but they don't work here (because of the read-only thing).

Subclassing? Hmm; worth a try, I guess. The key rejection takes place within the control's default processing code (within Win32), so keystrokes should be capture-able in the subclass routine before they get there.

However: If I try to SetFocus() on the (subclassed) edit control, won't that fail because the control is read-only? (I'll need to do that, otherwise keystrokes will go to the other edit control which is my editor's main edit window.)
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 09, 2024, 05:49:35 PM
Aha, I may have hit on it. Thinking a bit more outside the box, how about if I don't make the edit control read-only, but subclass it and capture all keystrokes sent to it. Then I can just filter out the ones I want (Ctrl-F & F3) and use those to trigger my search. Since no keystrokes can get to the actual control, it will effectively be read-only.

What do you think about that?
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: jj2007 on March 09, 2024, 09:36:14 PM
Quote from: sinsi on March 09, 2024, 03:37:35 PMAbout Keyboard Accelerators (https://learn.microsoft.com/en-us/windows/win32/menurc/about-keyboard-accelerators)
Or subclass the edit control?

Accelerators work, see attachment, subclassing doesn't because the disabled window receives zero messages.

GuiParas equ "Disabled window", w500, h900, icon Butterfly
include \masm32\MasmBasic\Res\MbGui.asm
  GuiControl MyEdit, "Edit", FileRead$("\Masm32\examples\exampl07\slickhuh\slickhuh.asm"),\
    font -14, +WS_DISABLED
  SetAccels F3:1
Event Accel
  MsgBox 0, Cat$('You want to find "'+Prompt$("Find:", "macros are evil")+'"'), "Hi", MB_OK
GuiEnd
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: sinsi on March 09, 2024, 09:40:34 PM
Quote from: jj2007 on March 09, 2024, 09:36:14 PMAccelerators work, see attachment, subclassing doesn't because the disabled window receives zero messages.
I thought read-only was different to disabled?
In another language I use you can still hilight and copy text from a read-only textbox.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: sinsi on March 09, 2024, 09:49:37 PM
Back to the OP, doesn't an accelerator get passed to the main window proc and not the edit control?
The edit control is really just something that doesn't directly participate in the process.
The main window gets a WM_COMMAND to tell it that F3 was pressed, it then pops up a dialog asking what to search for, then searches the text in the edit control, the edit is just a bystander having their insides rummaged through by an outsider.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: jj2007 on March 09, 2024, 09:58:25 PM
Quote from: sinsi on March 09, 2024, 09:40:34 PMI thought read-only was different to disabled?

Correct, if you use ES_READONLY instead of WS_DISABLED, subclassing is an option. Besides, you can SetFocus an ES_READONLY control, and it will receive WM_CHAR etc messages :thumbsup:
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 07:27:10 AM
Could someone please address my idea in this post (https://masm32.com/board/index.php?msg=127851)? Nobody's said anything about it yet.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: jj2007 on March 10, 2024, 07:33:06 AM
Quote from: jj2007 on March 09, 2024, 09:58:25 PMsubclassing is an option

Just try it out. Or do you want us to try it out?
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 07:46:43 AM
OK, I am doing so as I type. Just thought someone would have a thought or two. No biggie.

OK, 1st test done. Not good: I was able to capture Alt-F in my subclassed edit control proc, but not Ctrl-F. And the reason is that I do have a keyboard accelerator for that keystroke which takes precedence, so I get the expected behavior of the accel. (opens a Find dialog but for the main edit window, not my subclassed one). Which means, I guess, that I'd have to get rid of the accelerator to make this scheme work.

Unless someone has some other ideas ...

One complicated, kluge-y idea: Set a flag in the subclassed edit proc when it becomes "activated" (meaning user clicks on it), use that flag in the main proc. where the hotkey sends that acclerator. If that happens, send a private message to the subclassed edit proc telling it to do a search THERE instead of the main window.

Ugh. Messy.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: jj2007 on March 10, 2024, 08:16:50 AM
Quote from: NoCforMe on March 10, 2024, 07:46:43 AMopens a Find dialog but for the main edit window

Check the hwndOwner member of FINDREPLACE (assuming you use FindText). Besides, what is the problem searching inside hEdit instead of hMain?
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 08:43:27 AM
Welll, it's complicated:
Hmm, would that be fixed by making the "extra file view" windows RichEdits also?

Not sure what you mean by FindText. Is that a function? message (EM_FINDTEXT)? what?
And who uses FINDREPLACE?
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 09:20:51 AM
Another part of what I need to do here: find a reliable way to determine when a window (in this case an edit control) becomes "active", meaning the user clicks into it.

I've captured the messages received by my "extra file view" edit control upon mousing over the window and clicking on it:
(on move mouse over window)
Message: WM_SETCURSOR    hWin: 20406h, wParam: 20406h, lParam: 2000002h
Message: WM_NCMOUSEMOVE    hWin: 20406h, wParam: 2h, lParam: 1E100AEh
Message: WM_SETCURSOR    hWin: 20406h, wParam: 20406h, lParam: 2000002h
Message: WM_NCMOUSEMOVE    hWin: 20406h, wParam: 2h, lParam: 1ED00AEh
Message: WM_NCACTIVATE    hWin: 20406h, wParam: 1h, lParam: 403FEh
Message: WM_ACTIVATE    hWin: 20406h, wParam: 1h, lParam: 403FEh
Message: WM_IME_SETCONTEXT    hWin: 20406h, wParam: 1h, lParam: C000000Fh
Message: WM_IME_NOTIFY    hWin: 20406h, wParam: 2h, lParam: 0h
Message: WM_SETFOCUS    hWin: 20406h, wParam: 403FEh, lParam: 0h
Message: WM_KILLFOCUS    hWin: 20406h, wParam: C0018h, lParam: 0h
Message: WM_IME_SETCONTEXT    hWin: 20406h, wParam: 0h, lParam: C000000Fh
Message: WM_COMMAND    hWin: 20406h, sending ID: 1400, notification code: 256
Message: WM_NCMOUSELEAVE    hWin: 20406h, wParam: 0h, lParam: 0h

(on clicking on window)
Message: WM_SETCURSOR    hWin: 20406h, wParam: 20406h, lParam: 2000002h
Message: WM_NCMOUSEMOVE    hWin: 20406h, wParam: 2h, lParam: 1E300B8h
Message: WM_NCMOUSELEAVE    hWin: 20406h, wParam: 0h, lParam: 0h
Message: WM_NCACTIVATE    hWin: 20406h, wParam: 0h, lParam: 403FEh
Message: WM_ACTIVATE    hWin: 20406h, wParam: 0h, lParam: 403FEh
Message: WM_COMMAND    hWin: 20406h, sending ID: 1400, notification code: 512
It looks as if WM_SETCURSOR is my friend here, yes? Or should I use WM_ACTIVATE (wParam = 0) instead?
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: jj2007 on March 10, 2024, 10:16:21 AM
Quote from: NoCforMe on March 10, 2024, 08:43:27 AMNot sure what you mean by FindText. Is that a function?

Yes, see Win32.hlp, or google it.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 11:33:39 AM
Goddamnit, JJ. That's just not helpful. "Just Google it", the most lazy-ass suggestion in the world.

Let me tell you what's wrong with your reply:
and on and on.

If you're going to help, then help. Otherwise, please just STFU.

I'm not asking for people to do my homework for me here, just looking to get pointed in the right direction.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: stoo23 on March 10, 2024, 11:51:13 AM
Win32.hlp: What is that? Where is that?

.zip file available here:
Win32.hlp (https://masm32.com/files/win32.zip)
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: jj2007 on March 10, 2024, 12:11:26 PM
Quote from: NoCforMe on March 10, 2024, 11:33:39 AM"Just Google it", the most lazy-ass suggestion in the world.

So you mean I should google it for you?

Quote from: NoCforMe on March 10, 2024, 11:33:39 AMHave you tried "googling" (hate that verb) FindText?

No, I tried googling FindText winapi, and found it right on top of the list.

@stoo: Thanks, I had forgotten it was there. I'm not sure whether it's the same version as the one at the Modula link (https://www.modula2.org/win32tutor/references.php). Anyway, David might have to look for the *.chm version because *.hlp won't run on his machine.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 12:20:48 PM
Quote from: jj2007 on March 10, 2024, 12:11:26 PMAnyway, David might have to look for the *.chm version because *.hlp won't run on his machine.
Yep, .hlps don't run here. So where do I get Win32.chm?
(Yes, I know there's a work-around for .hlp files, but I don't have it and don't feel like chasing it down at the moment.)
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: stoo23 on March 10, 2024, 12:27:22 PM
Quote@stoo: Thanks, I had forgotten it was there. I'm not sure whether it's the same version as the one at the Modula link (https://www.modula2.org/win32tutor/references.php)
Well, it IS the Exact same "Modula" version, ...  :smiley: as I utilised the 'link' you provide to acquire it  :smiley:  :thumbsup:
It has ONLY recently been Put there by myself  :wink2:  :smiley:  (as I was 'Updating' an OLD 'links' Page Hutch' had created Ages ago) and was lingering about on the Server, see here:
https://masm32.com/website/index.htm (https://masm32.com/website/index.htm)

I will eventually,... sigh, .... Add it to the existing Masm32SDK Web Page.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: stoo23 on March 10, 2024, 12:29:50 PM
QuoteSo where do I get Win32.chm?

Hang on a Minute ... or Three ....  :rofl:
I am pretty sure I already have a Copy loaded on the Server and if Not, it soon Will be  :smiley:

Stay Tuned,..  :wink2:
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: sinsi on March 10, 2024, 12:46:31 PM
Quote from: NoCforMe on March 10, 2024, 08:43:27 AMAnd who uses FINDREPLACE?
Well, FindText for one  :biggrin:

Quote from: NoCforMe on March 10, 2024, 09:20:51 AMAnother part of what I need to do here: find a reliable way to determine when a window (in this case an edit control) becomes "active", meaning the user clicks into it.
Could you ask questions that have nothing to do with the thread in a new thread? FWIW, I would have thought WM_ACTIVATE.
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: stoo23 on March 10, 2024, 01:02:14 PM
Here you go  :smiley:

Win32.chm (https://masm32.com/files/win32(03-11-2017).7z)

NB: It's a 7zip Archive
Title: Re: How to capture keystrokes in a window with a read-only edit control
Post by: NoCforMe on March 10, 2024, 01:24:02 PM
Got it. Thanks!