Recent

Author Topic: How to return special keys (e.g., Alt+H)  (Read 17661 times)

dculp

  • Full Member
  • ***
  • Posts: 129
Re: How to return special keys (e.g., Alt+H)
« Reply #15 on: April 25, 2017, 01:52:06 pm »
Molly --

Quote
Again, do note that certain key combinations are simply 'hijacked' by Windows and will never reach your code.

I considered this. However, Ctrl+O (open-file) isn't hijacked so I assume that Ctrl+S (save-file) would also not be hijacked. Is there any way to determine if a key combination is being hijacked?

In any case, any work-around for Ctrl+S?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to return special keys (e.g., Alt+H)
« Reply #16 on: April 25, 2017, 02:33:12 pm »
I considered this.
I doubt it, otherwise you would not have come up with the queston ;-)

Quote
However, Ctrl+O (open-file) isn't hijacked
afaik ctrl-o has no special meaning inside cmd.exe or windows in general. But, for instance i use a special cmd.exe that has all sort of additions such as see through, multiple tabs etc. and there ctrl-o is hijacked (at least when not changed the default keyboard shortcuts).

Quote
so I assume that Ctrl+S (save-file) would also not be hijacked.
ctrl-s is stopping output inside a cmd.exe console, which afaik is default behaviour

Quote
Is there any way to determine if a key combination is being hijacked?
ctrl break, ctrl-c, ctrl-f1, ctrl-print-screen and a bunch more is hijacked by windows. That is because processed input flag is set on the input handler (i still have to verify this to make 100% sure).

I do not know of an existing list of shortcuts that are in place, but it also depends on your personal windows settings. e.g. you can choose to 'swap' ctrl-f1 for something else.

Quote
In any case, any work-around for Ctrl+S?
See the link i posted.

I am currently testing with fpc (but no windows around, so have to search for a machine which can take me a while). Also i have no clue how this would otherwise influence Bart's example code as afaik that depends on processed input.

The other alternative is writing your own (windows console) keyboard driver which does as you want.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to return special keys (e.g., Alt+H)
« Reply #17 on: April 25, 2017, 03:08:37 pm »
Two routines that might be able to help you out:

Code: Pascal  [Select][+][-]
  1. function IsProcessed: boolean;
  2. var
  3.   hConsoleInput : THandle;
  4.   dwMode : DWORD;
  5.   retVal : BOOL;
  6. begin
  7.   Result := true;
  8.  
  9.   Reset(Input);
  10.   hConsoleInput := TTextRec(Input).Handle;
  11.   retVal := GetConsoleMode(hConsoleInput, dwMode);
  12.   if (retVal) then
  13.   begin
  14.     if (dwMode and ENABLE_PROCESSED_INPUT <> 0)
  15.     then Result := true
  16.     else Result := false;
  17.   end
  18.   else WriteLn('Error: call to GetConsoleMode failed. Returned value is unreliable');
  19. end;
  20.  
  21. procedure ManipulateInput;
  22. var
  23.   hConsoleInput : THandle;
  24.   retVal : BOOL;
  25.   dwMode : DWORD;
  26. begin
  27.   Reset(Input);
  28.   hConsoleInput := TTextRec(Input).Handle;
  29.   retVal := GetConsoleMode(hConsoleInput, dwMode);
  30.   if (retVal) then
  31.   begin
  32.     if (dwMode and ENABLE_PROCESSED_INPUT <> 0) then
  33.     begin
  34.       WriteLn('Processed input flag is currently set. Now attempting to disable');
  35.       dwMode := dwMode and not(ENABLE_PROCESSED_INPUT);
  36.       retVal := SetConsoleMode(hConsoleInput, dwMode);
  37.       if (retVal) then
  38.       begin
  39.         Write('Verifying status of ENABLE_PROCESSED_INPUT flag:');
  40.         if IsProcessed
  41.         then WriteLn(' Flag is ON')
  42.         else WriteLn(' Flag is OFF')
  43.       end
  44.       else WriteLn('Error: call to SetConsoleMode failed');
  45.     end
  46.     else WriteLn('Processed input flag was not set. There was nothing to change');
  47.   end
  48.   else WriteLn('Error: call to GetConsoleMode failed');
  49. end;
  50.  

Just before you make a call to InitKeyBoard() you can call IsProcessed() to see the current input handler status.

You can make a call to ManipulateInput() ( also before calling InitKeyBoard() ) to change the inputmode. Logically, you can make a call to IsProcessed again to make sure the ENABLE_PROCESSED_INPUT flag  is set or not.

I have no idea if it works though, sorry for that.

dculp

  • Full Member
  • ***
  • Posts: 129
Re: How to return special keys (e.g., Alt+H)
« Reply #18 on: April 25, 2017, 06:02:49 pm »
Quote
You might be able to try this but have no clue if that is too late to do inside your code for default windows keyboard driver.

Somehow I missed the "this" link so I misunderstood. Sorry.

I'm running from an unaltered Win7 cmd.exe console.

Quote
I do not know of an existing list of shortcuts that are in place, but it also depends on your personal windows settings. e.g. you can choose to 'swap' ctrl-f1 for something else.

I normally run with the CapsLock and Ctrl keys swapped. I thought this might be causing a problem so I unswapped them and rebooted. Same result.

If Bart's code works correctly under Linux then this would indicate that Windows is indeed hijacking Ctrl+S.

I'll look at your additional code later. Thanks.


molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to return special keys (e.g., Alt+H)
« Reply #19 on: April 25, 2017, 06:20:30 pm »
If Bart's code works correctly under Linux then this would indicate that Windows is indeed hijacking Ctrl+S.
Note that there is not something like a console cross-platform solution.

For that the console for all platforms simply differ too much. As stated, i use a replacement console and there other keys are "hijacked". Also note that there is a reason not any (windows console) program uses ctrl short-cuts rather alt- ones. But, my memory might be wrong on that.

Quote
I'll look at your additional code later. Thanks.
I forgot to mention that in order for these two routines to work you need to include unit windows in your uses clause.

Please in case you are abel too then check my solution and report back if it worked or not. If it doesn't work then we need to find another solution (besides not using ctrl for shortcut's or using a console at all, which btw woul dbe my first suggestion ;-) )

dculp

  • Full Member
  • ***
  • Posts: 129
Re: How to return special keys (e.g., Alt+H)
« Reply #20 on: April 25, 2017, 09:41:52 pm »
Molly --

Calling your code before InitKeyBoard --
Quote
IsProcessed (before ManipulateInput) = TRUE
Processed input flag is currently set. Now attempting to disable
Verifying status of ENABLE_PROCESSED_INPUT flag: Flag is OFF
IsProcessed (after ManipulateInput) = FALSE

However, still same result (no response to Ctrl+S). Ctrl+S is trapped in
Code: Pascal  [Select][+][-]
  1. KE := GetKeyEvent;
--> no writeln output until two subsequent "acceptable" keys are pressed (the first subsequent key doesn't show any output; the second subsequent key acts normally).

BTW, I'm modifying an old TP7 console program to run under Win7+. I'm making a few upgrades. However,  the program has over 150 menus so changing all of these to Windows would be a major task (not worth the effort).
« Last Edit: April 25, 2017, 09:50:40 pm by dculp »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to return special keys (e.g., Alt+H)
« Reply #21 on: April 25, 2017, 10:49:42 pm »
@dculp:
Ok, thank you for testing and reporting back.

The output shows that the routines do what they are suppose to do.

Sorry that it didn't work. It was a long-shot.

It seems that ctrl-s is a special case (pause terminal output) :-S

Uhm... that leaves us a bit out of options. Setting a keyboard handler for a console can't be done because we're missing a threadid.

That is, unless you stuff the keyboard hook into a dll and open that (which is a bit cumbersome).


You could install a global keyboard hook but, that works for all your windows applications, which is not really advisable.

Another option could perhaps be to allocate your own console window and work your way up from there, e.g. add your own message handling loop etc.

I think i have to call defeat here  :'(. Perhaps someone else knows a better solution and/or have better suggestions for your case.

dculp

  • Full Member
  • ***
  • Posts: 129
Re: How to return special keys (e.g., Alt+H)
« Reply #22 on: April 26, 2017, 01:03:58 am »
GetKeyEvent checks for key combinations (Ctrl+a, etc.). However, there might be a more primitive method to precheck for these combinations (i.e., if I were trying to build-my-own). However, I don't know how to implement.

Code: Pascal  [Select][+][-]
  1. begin
  2. if Ctrl_key_down then
  3.    begin
  4.    if S_key_down then
  5.       Do_something;
  6.    end
  7.  
  8. else // handle all other keys normally
  9.    begin
  10.    KE:= GetKeyEvent;
  11.    ...
  12.    end;
  13. end;      
  14.  

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to return special keys (e.g., Alt+H)
« Reply #23 on: April 26, 2017, 02:57:23 am »
fwiw the ctrl-s combination is not retrieved by the keyboard handle/driver *at all* and the code used so far (both Bart's and mine) show that it can't be done the way you intend to.

You can send m$ a big thank you for that, as they make it so cumbersome for things that ought to be simple to accomplish.

Want to see more: try pressing Ctrl-esc or alt-esc. Another nice one: alt-F4 :-)

As said, you can overcome this (for windows only) by allocating your own console so that you are able to install your own custom keyboard handler which is tied to your process (instead of a generic/global console). Which imho is another glitch from m$ in making things utterly complicated when it comes to working with console-mode.

also fwiw: i was able to test my code for myself now as well and indeed the ctrl-s combination is not received.

The answer as posted on the SO link is therefor wrong (sorry for that as i did not know beforehand) and is just a fairy-tale (someone should either downgrade the answer or code should be shown as otherwise the answer is utter crap and wasting people's time, just as many of the other answers related to your question).

As said, maybe i'm looking at it completely wrong, so let's hope someone else is able to help you out there. But, i fear it won't be an easy answer.

dculp

  • Full Member
  • ***
  • Posts: 129
Re: How to return special keys (e.g., Alt+H)
« Reply #24 on: April 27, 2017, 04:43:12 pm »
Still not completely resolved but I appreciate the significant efforts.

 

TinyPortal © 2005-2018