I have a WH_GETMESSAGE hook that catches WM_KEYDOWN and WM_INPUT (right now only WM_KEYDOWN), what i need is a way to measure time intervals between WM_KEYDOWNs to see how fast user typing. Exactly i need to drop entire keystroke if it was inputed too fast (i.e. input wasn't actually from user, but from some HID keyboard device, like barcode scanner). What i need to do is for each symbol i'm gonna wait for 30 ms and then if none further symbols applied i need to send it to window it was meant for. And that check need to be performed on each WM_KEYDOWN. If new symbol applied i will push it to my queue along with any further symbols that will go within 30 ms limit between messages (actually i tested on my computer it takes something like 31-32 ms to input symbol and barcode scanner needs something like 15-16 ms which will be sufficent).
I figured out following algorithm:
1. If incoming message is WM_KEYDOWN - store it (with time offcourse).
2. If next incoming message isn't WM_KEYDOWN or time-limit exceeded - send stored message to it's recipient and store current message if it's WM_KEYDOWN - go to step 1.
3. If next message came in 30 ms limit - store it too.
4. If next message is VK_RETURN or sequence is longer than 128 symbols - drop current sequence and store it as barcode. Goto step 1.
Current flaw in this algorithm is that sendMessage thing. I used both sendMessage and postMessage but they both just resend it to my hook procedure (which is obvious i guess). Is there any way to just send this message directly to window it was mento to be send?
Or maybe there are other solution for my problem?
My explanation may seem ambiguous so i just write what i need to achieve:
I need to intercept barcode scanner input before it actually reaches window procedure, so no WM_KEYDOWN messages from scanner will reach target window. A barcode message are typically comes with 15-16ms interval between single WM_KEYDOWNs and ends with VK_RETURN.
Also i need to use exactly WH_GETMESSAGE (or similiar hook that will allow me to catch both WM_INPUT and WM_KEYDOWN). WM_INPUT is needed to distinguish input from various devices (basically i'm going to enum all HID keyboards, determine which of them send barcodes if such event occurs i will flag device as barcode scanner and store it's VID&PID to local database and it will be use for other).
Related
Is it possible to have AT+CMGS commands cancelled by some control code other than ESC?
I need it because ESC is intercepted by the attached equipment for its own use and never gets to the modem. And, I can't change that.
Unfortunately, CTRL-Z will send even an empty message, or else I could backspace enough to clear the message and do CTRL-Z to abort.
The relevant "AT command set" manual is no help.
According to TS 127 005 specification, it seems that there's no way to configure the character for SMS sending abortion.
Anyway I can suggest a workaround, based on three different commands:
+CMGW - Write Message To Memory
+CMGD - Delete Message
+CMSS - Send Message From Storage
So basically, instead of using +CMGS that sends the message in one step
Write the SMS to memory with +CMGW (same syntax of +CMGS). After the SMS contents closure with the CTRL-Z character, its answer is
+CMGW: <index>
where <index> is the message location index in the current memory storage
Actually send it with
AT+CMSS=index
Delete the SMS with
AT+CMGD=index
Since memory slots are limited, you will have to delete it anyway. If you realize that the message you are composing during +CMGW phase is wrong, store it anyway with CTRL-Z and skip the actual sending.
As you can see, the entire procedure is performed without using the ESC character (0x1B), can be easily automated and doesn't require much more time to be executed.
The separate thread is a game app over which I have no control other than sending keys (keydown and keyup messages). I can do that when I attach my threat to the game.
My question is, how can I determine when the game has processed a key (down, up)?
I have tried using peekmessage with NOREMOVE but that always returns 0 (there is never a key waiting).
I am trying to get the fastest response in playing the game.
I currently am using a heuristic of about 130 ms after sending a keydown before I send a keyup. That works most of the time but is not 100% reliable. I need a reliable means of sending keys.
What are my options?
-Doug
I am sending files (up to 100Mo on my android handled) using the Channel Api.
I decided to create a handler to update the progress of the transfer to that the user is aware of the progress.
I use the Message Api to send the file size to my handled and I update the progress checking each x milliseconds the size of the file.
The matter is that I don't know first if that's a good way of doing what I want, and second, due to the fact that it's asynchronous, I have to wait that I correctly received the file size in the onMessageReceived before sending the file.
If you are using ChannelApis, you can use the low level version of transfer (using output stream and input stream) and then on the sender side, you can update your progress bar with the amount that you are writing to the output stream. If you are using sendFile() method, you don't have any view into the progress on the sender side so you need to report that back using, say, Message Apis as you are doing. Instead of doing that at x milliseconds, you may decide to make it a bit smarter; if you have the size of the whole file, you probably wouldn't want to send a message if the visual change in the progress bar is not going to be much or noticeable; in other words, try to reduce the number of communications as much as possible.
Simple task: Send a windows message with dynamically allocated data, e.g. an arbitrary length string. How would you manage the responsibility to free this data?
The receiver(s) of the windows message could be responsible to free this data. But: How can you guarantee that all messages will actually be received and thus the linked data will be freed? Imagine the situation that the receiver is shutting down, so it won't process it's message queue any more. However, the message queue still exists (for some time) and can still accept messages, which won't be processed any more.
Thanks!
PostMessage returns a BOOL that tells you whether the message was posted or not. This is usually good enough, because your window should be valid until it receives the WM_DESTROY and the WM_NCDESTROY messages. After a call to DestroyWindow (which sends these messages) you should not be able to successfully call PostMessage again.
Now, if your PostMessage returns FALSE you have to clean up. If it doesn't, the window procedure has to clean up. Don't send messages that have to be cleaned up to random windows that might not handle them. Actually, don't send any WM_USER + x messages to any windows you don't handle.
There's nothing to do here. As soon as the call to SendMessage returns, you can free the data. As it happens, the other app isn't looking at your memory anyway since it's in a different process. Instead Windows marshals the data across the process boundary.
What's more, if you are receiving the data in a WndProc, you can't take a copy of the pointer to the string. Instead you must take a copy of the contents of the string since that pointer is only valid for the duration of that call to WndProc.
The other point to make is that you have a confusion about the message queue. When you send a message, that happens synchronously and the queue is not involved. The message queue is where posted messages are placed. They are process asynchronously.
In a Win32 C++ application, we start a message loop that fetches messages from a queue, translates them and then dispatches them. Eventually, each message reaches our WndProc where the associated event can be handled.
I understand that part. What I don't understand is the in between goings-on. Specifically:
Different kinds of OS interrupt handlers must be placing messages in the said 'message queue', but where within the process address space does this queue reside? How is it exposed to the interrupt handler code?
What does it mean to 'translate' the message? What does the call to TranslateMessage() really do?
Once dispatched by DispatchMessage(), what all places does the message swing by before reaching my WndProc (i.e. what does the OS do with it)?
If anyone knows the answers to the above, kindly satisfy my curiosity. Thanks.
The OS maintains a message queue, where it puts the events (e.g., from interrupts or other sources). It then sends the messages from that queue to all windows, depending on the message (e.g., it won't send key messages to a window that doesn't have focus).
Applications can have their own queue to process messages. Those queues are created on request (only if needed).
Translating a message is used to create messages that are not 'real' events. For example, the WM_CONTEXTMENU message is 'translated' from either a mouse right-click, or the context menu key, or shift-F10. The WM_CHAR is translated from WM_KEYDOWN messages. And of course many other messages are 'translated' that way.
A message is posted to every window that should receive it. The OS decides depending on the type of message whether a window should receive that message or not. Most messages are waited for by the system, i.e., the message won't get posted to another window until it was processed by the window. This has a great impact for broadcast messages: if one window doesn't return when handling that message, the queue is blocked and other windows won't receive the message anymore.
It depends on how your message is sent and how it's handled.
When you call SendMessage, if the target window is owned by the current thread, the call bypasses the message queue for the window and the window manager directly calls the windowproc on the target window. If the target window is owned by another thread, the window manager effectively calls PostMessage and pumps window messages until the target window returns from the window proc.
When you call PostMessage, the window manager marshals the message parameters and inserts the corresponding object onto the message queue for the target window. When it next calls GetMessage the message is removed from the message queue.
The window manager also registers for raw input events from the input devices (keyboard and/or mouse) and it generates messages for those input events. It then inserts those messages in the queue as appropriate (the processing of input events is complicated because it depends on what messages are already in the message queue for the window).
As Stefan indicated, TranslateMessage just translates accelerator keys - for instance it converts key sequences to WM_COMMAND messages.
Different kinds of OS interrupt handlers must be placing messages in the said 'message queue', but where within the process address space does this queue reside? How is it exposed to the interrupt handler code?
Windows are associated with threads. Each thread with a window has a thread queue in the process's address space. The OS has an internal queue in its own address space for the hardware-generated events. Using details of the event and other state information (e.g., which window has the focus), the OS translates the hardware events into messages that are then placed in the appropriate thread queue.
Messages that are posted are placed directly in the thread queue for the target window.
Messages that are sent are usually processed directly (bypassing the queue).
The details get hairy. For example, the thread queues are more than lists of messages--they also maintain some state information. Some messages (like WM_PAINT) aren't really queued, but synthesized from the additional state information when you query the queue and it's empty. Messages sent to windows owned by other threads are actually posted to the receiver's queue rather than being processed directly, but the system makes it appear like a regular blocking send from the caller's point of view. Hilarity ensues if this can cause deadlock (because of circular sends back to the original thread).
The Jeffrey Richter books have a lot (all?) of the gory details. My edition is old (Advanced Windows). The current edition seems to be called Windows via C/C++.
The OS does a LOT of work to make the message stream appear rational (and relatively simple) to the caller.
What does it mean to 'translate' the message? What does the call to TranslateMessage() really do?
It watches for virtual key messages and, when it recognizes a key-down/key-up combination, it adds character messages. If you don't call TranslateMessage, you won't receive character messages like WM_CHAR.
I suspect it sends the character message directly before returning (as opposed to posting them). I've never checked, but I seem to recall that the WM_CHAR messages arrive just before the WM_KEYUP.
Once dispatched by DispatchMessage(), what all places does the message swing by before reaching my WndProc (i.e. what does the OS do with it)?
DispatchMessage passes the message to the WndProc for the target window. Along the way, it some hooks may get a chance to see the message (and possibly interfere with it).
To address the last subquestion, a dispatched message will go to your WindowProc after it's been piped through all hooks (WH_CALLWNDPROC)
Not absolutely positive about this but my best guess says:
The queue is a system object that you access with Win32 API calls. It is not in your process address space at all. So the interrupt handlers can access it (probably through the HAL (Hardware Abstraction Layer) of the kernel).
In Win16, that call took the various subparts of a bigger message and mashed them into a whole. So TranslateMessage would add the WM_KEYPRESS when it found the corresponding WM_KEYDOWN WM_KEYUP sequence. It would also turn various button click messages into doubleclick messages based on internal setting and the timestamps of the messages. Whether it still does this in Win32, I don't know.
DispatchMessage is probably where Window message hooks get processed. So if there's a hook on your window, it is either called here or when the GetMessage is called. I'm not sure. Other than that, DispatchMessage just looks up the WndProc address associated with the window and calls it. There's not much else for it to do.
Hope that helps.