Pages: [1]
  Print  
Author Topic: keyboard_string_direct  (Read 988 times)
Offline (Male) time-killer-games
Posted on: December 11, 2014, 11:34:26 AM

Member
Location: Virginia Beach
Joined: Jan 2013
Posts: 1067

View Profile WWW Email
I need a keyboard_string variable that responds to keyboard input even when the window isn't in focus, similar to how keyboard_check_direct() works.

Thanks.
Logged
Offline (Male) Goombert
Reply #1 Posted on: December 11, 2014, 04:24:30 PM

Contributor
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2992

View Profile
We don't actually query the keyboard when not in focus, and there's no way to because we don't receive messages, this is by design of Windows. So you would have to make sure you don't have the option to freeze when losing focus set. Then you would have to do it manually with keyboard_check_direct

I actually found a result on google for what you are looking for, but its in Dutch or something.
http://www.game-maker.nl/forums/topic,53927.5/imode

Code: (GML) [Select]
for(i=5; i<255; i+=1){
   if(keyboard_check_direct(i)){
      string += chr(i);
   }
}

Allow me to give you a modified version that handles backspace:
Code: (GML) [Select]
for (i = 5; i < 255; i += 1) {
  if (keyboard_check_direct(i)){
    if (i = vk_backspace) {
      keyboard_string_direct = string_copy(keyboard_string_direct, 0, string_length(keyboard_string_direct)-1);
    } else {
      keyboard_string_direct += chr(i);
    }
  }
}
Logged
Welcome to ENIGMO, the game engine built by fucking aliens.

Offline (Male) time-killer-games
Reply #2 Posted on: December 11, 2014, 10:50:17 PM

Member
Location: Virginia Beach
Joined: Jan 2013
Posts: 1067

View Profile WWW Email
Thank you Bob!! This is a very rarely needed feature so a quick code snippet is a lot better than actually implimenting it, last thing we need is more useless "if" checks. I tried your code and since its detected whenever held down  every step, instead of each press/release, it types really fast so fast pressing a key for a 10th of a second will type that char about 4 times, though I fixed this by changing the room speed to 10 and it lead to the desired result. thank you!!
Logged
Offline (Male) Goombert
Reply #3 Posted on: December 11, 2014, 11:10:42 PM

Contributor
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 2992

View Profile
Yeah I noticed that I was experimenting locally, it's because the other keyboard functions are registered by window messages for keyup keydown etc. I do not know why it is that the keyboard does its thing the way that it does, but usually pressing a key and holding it types the letter then waits a bit and starts spitting the letters out. One thing I tried was making a keyboard_status[255] array to check against and setting either 0 for not pressed, 1 for pressed, 2 for intermediate, 3 for pressed, and 4 for released. That's easy enough to do and you could consider 1 and 3 for down, but it still don't function quite the same.

The other problem is that the input string is always uppercase, I don't know if you noticed that or not. So you would need to manually check vk_shift yourself to determine whether the keys are uppercase or lowercase. Perhaps ENIGMA could at least provide a helper function for this though. We don't have this problem with our built in keyboard_string because the API's just tell us what the last char was.
https://github.com/enigma-dev/enigma-dev/blob/master/ENIGMAsystem/SHELL/Platforms/Win32/WINDOWScallback.cpp#L175
https://github.com/enigma-dev/enigma-dev/blob/master/ENIGMAsystem/SHELL/Platforms/xlib/XLIBmain.cpp#L88

The better solution would be to write a dll extension like you did for Window flags that hooks onto the system message loop.
http://msdn.microsoft.com/en-us/library/ms644990%28v=VS.85%29.aspx

You could borrow some of ENIGMA's code and replicate the input system to behave the exact same way except work when not in focus. You could have functions like keyboard_check_pressed_direct and keyboard_check_released_direct to get around overloading.
Logged
Welcome to ENIGMO, the game engine built by fucking aliens.

Pages: [1]
  Print