Tk_CreateGenericHandler(3tk)
π Tk_CreateGenericHandler β the manual, but make it make sense π
okay so bestie, we're in the Tk Library Procedures section of the 3tk manual, and today's main characters are Tk_CreateGenericHandler and Tk_DeleteGenericHandler. pull up a chair, grab a strawberry π, and let's get into it.
β¨ NAME β¨
Tk_CreateGenericHandler, Tk_DeleteGenericHandler β associate a procedure callback with all X events. like, all of them. every single one. no filter, no cap π
π SYNOPSIS π
#include <tk.h>
Tk_CreateGenericHandler(proc, clientData)
Tk_DeleteGenericHandler(proc, clientData)
π ARGUMENTS π
Tk_GenericProc *proc(in) β the procedure we're gonna invoke whenever any X event happens, on any display. yes, any. it's giving unhinged but it's valid.void *clientData(in) β an arbitrary one-word value that gets passed straight toproc. it's your little carry-on bag of vibes π«Ά
π¦ DESCRIPTION π¦
So. Tk_CreateGenericHandler arranges for proc to be invoked in the future whenever any X event occurs. iconic, right?
β οΈ but listen β this mechanism is not intended for dispatching X events on windows that Tk actually manages. if that's what you want, you should use Tk_CreateEventHandler for that purpose, periodt. don't come crying to me.
Tk_CreateGenericHandler is for other purposes, like:
- tracing X events π
- monitoring events on windows that aren't owned by Tk
- accessing X-related libraries that were never originally designed to work with Tk
- and so on, and so forth, the whole list honestly
The callback to proc gets made by Tk_HandleEvent. and this is the important bit, bestie: this mechanism only works in programs that dispatch events through Tk_HandleEvent (or through other Tk procedures that call Tk_HandleEvent, such as Tcl_DoOneEvent or Tk_MainLoop). no Tk_HandleEvent in your pipeline? no callback. simple as that. π
proc should have arguments and a result that match the type Tk_GenericProc:
typedef int Tk_GenericProc(
void *clientData,
XEvent *eventPtr);
The clientData parameter to proc is a copy of the clientData argument you handed to Tk_CreateGenericHandler back when the callback was created. typically, clientData points to a data structure holding application-specific info about how to handle events. and eventPtr is a pointer to the X event itself. β¨
Whenever an X event is processed by Tk_HandleEvent, proc gets called. the return value from proc is normally 0. a non-zero return value means the event is not to be handled further β i.e. proc has done all the processing that's to be allowed for that event. so returning non-zero is you saying "I ate, we're done here, move on" π½οΈ
If there are multiple generic event handlers, each one is called for each event, in the order in which they were established. first come, first called. no queue-jumping. π
Tk_DeleteGenericHandler may be called to delete a previously-created generic event handler: it deletes every handler it finds that matches the proc and clientData arguments. if no such handler exists, then Tk_DeleteGenericHandler just returns without doing anything β no drama, no error, just vibes. π§
π although Tk supports it, it's probably a bad idea to have more than one callback with the sameprocandclientDataarguments. like, technically fine. morally questionable. iykyk.
And the caveat that matters: establishing a generic event handler does nothing to ensure that the process will actually receive the X events the handler wants to process. for example, it's the caller's responsibility to invoke XSelectInput to select the desired events, if that's necessary. the handler doesn't manifest events out of thin air, bestie. do your part. π
π₯ KEYWORDS π₯
bind, callback, event, handler
and that's the whole page, bestie β you're officially fluent in generic event handlers π
later babes, this was the Tk_CreateGenericHandler(3tk) page from the Tk library docs. slay, stay safe, and always call Tk_HandleEvent πβ¨
π¦ where this page comes from
- debian trixie: tk9.0-doc 9.0.1-3
- ubuntu questing: tk9.0-doc 9.0.2-1
- ubuntu resolute: tk9.0-doc 9.0.3-1
- ubuntu stonking: tk9.0-doc 9.0.4-1
- Copyright (c) 1992-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms