Here I was more wondering about how a task panel implementation can provide consistent behaviour regardless of where the mouse pointer currently is when a state altering key gets pressed.
It doesn't depend on where the mouse cursor is but which widget has the focus. You can e.g. open the sketcher click into the 3d view to be sure it has the focus, move the cursor to the task panel and then click ESC. In this case it goes through ViewProvider::eventCallback.
Then you can test the opposite. Open the sketcher click on the task panel and move the cursor to the 3d view. If you now click ESC it goes through TaskView::reject.
sliptonic's tongue in cheek video of the UI made me realise that at least some of those should be fixed. And I gotta admit the most annoying one for me personally is hitting Escape in the Sketcher and it closes the entire TaskPanel.
IMO, the main point here is that when you clicked on one of the sketcher functions and press ESC it should only leave the function mode you are in but leave the sketcher open.
Btw, when you clicked on of the sketcher functions, right-clicked to leave it again and then restart this or another function pressing ESC only leaves this function mode again but doesn't close the sketcher. So, there must happen something on right-click that "heals" this logic.
However, when you opened the sketcher and don't use (or left) its functions then it's IMO correct to leave the sketcher. This way you're much more efficient because you don't have to move the mouse cursor over your whole screen in order to click on Close.