Jump to content
TroisVerites

What KeyDown use to send the key to windows ?

Recommended Posts

I tried this with scar 3.36 , but it doesn't work with my game X.

 

I use this in other game Z and it work (scar 3.34).

 

I tried the SendInput API (KeyCode=2) in another langage (Python) and it work too with game X.

 

I use the same user account for game and test program.

 

Of course if I try all in notepad, everything is fine.

 

Anyone has an idea ?

 

 


program New;

{$I .\Math.scar}

begin
  WaitRR(5000, 5000);
   KeyDown('1');
   KeyUp('1');
   vKeyDown(49);
   vKeyUp(49);
end.

Link to comment
Share on other sites

For Freddy,

 


#-------------------------------------------------------------------------------
# Name:        os_win_sendinput
# Purpose:
#
# Author:      Fred
#
# Created:     08/11/2012
# Copyright:   (c) Fred 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

#-------------------------------------------------------------------------------
# import
#-------------------------------------------------------------------------------
from ctypes import *
user32 = windll.user32

#-------------------------------------------------------------------------------
# import for main
#-------------------------------------------------------------------------------
import time

#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# START SENDINPUT TYPE DECLARATIONS
PUL = POINTER(c_ulong)

class KeyBdInput(Structure):
   _fields_ = [("wVk", c_ushort),
            ("wScan", c_ushort),
            ("dwFlags", c_ulong),
            ("time", c_ulong),
            ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
   _fields_ = [("uMsg", c_ulong),
            ("wParamL", c_short),
            ("wParamH", c_ushort)]

class MouseInput(Structure):
   _fields_ = [("dx", c_long),
            ("dy", c_long),
            ("mouseData", c_ulong),
            ("dwFlags", c_ulong),
            ("time",c_ulong),
            ("dwExtraInfo", PUL)]

class Input_I(Union):
   _fields_ = [("ki", KeyBdInput),
             ("mi", MouseInput),
             ("hi", HardwareInput)]

class Input(Structure):
   _fields_ = [("type", c_ulong),
            ("ii", Input_I)]

# END SENDINPUT TYPE DECLARATIONS

#-------------------------------------------------------------------------------
# Class
#-------------------------------------------------------------------------------
class win_sendinput:
   """
   win_sendinput is a class for use of the function sendinput.
   """

   #---------------------------------------------------------------------------
   # Class constants
   #---------------------------------------------------------------------------
   INPUT_MOUSE = 0
   INPUT_KEYBOARD = 1
   INPUT_HARDWARE = 2

   KEYEVENTF_KEYDOWN = 0
   KEYEVENTF_KEYUP = 2
   KEYEVENTF_SCANCODE = 8

   MOUSEEVENTF_MOVE = 0x0001
   MOUSEEVENTF_LEFTDOWN = 0x0002
   MOUSEEVENTF_LEFTUP = 0x0004
   MOUSEEVENTF_RIGHTDOWN = 0x0008
   MOUSEEVENTF_RIGHTUP = 0x0010
   MOUSEEVENTF_MIDDLEDOWN = 0x0020
   MOUSEEVENTF_MIDDLEUP = 0x0040
   MOUSEEVENTF_ABSOLUTE = 0x8000
   #---------------------------------------------------------------------------
   # Mouse primitive functions for windows-
   #---------------------------------------------------------------------------
   @staticmethod
   def MouseInputSend(Flag,dx,dy):
       """
       """
       extra = c_ulong(0)
       Inp = Input()
       Inp.type = win_sendinput.INPUT_MOUSE
       Inp.ii.mi = MouseInput(dx, dy, 0, Flag, 0, pointer(extra))
       Size = sizeof(Inp)
       user32.SendInput(1, pointer(Inp), sizeof(Inp))

   @staticmethod
   def KeyInputSend_vkey(Flag, Key):
       """
       """
       Inp = Input()
       Inp.type = win_sendinput.INPUT_KEYBOARD
       Inp.ii.ki.dwFlags = Flag
       Inp.ii.ki.wVk = Key
       Size = sizeof(Inp)
       user32.SendInput(1, pointer(Inp), sizeof(Inp))


   @staticmethod
   def KeyInputSend_ScanCode(Flag, Key):
       """
       """
       Inp = Input()
       Inp.type = win_sendinput.INPUT_KEYBOARD
       Inp.ii.ki.dwFlags = win_sendinput.KEYEVENTF_SCANCODE | Flag
       Inp.ii.ki.wScan = Key
       Size = sizeof(Inp)
       user32.SendInput(1, pointer(Inp), sizeof(Inp))



#-------------------------------------------------------------------------------
# TEST
#-------------------------------------------------------------------------------
def main():
   time.sleep(5.0)
   # Virtual Key - Key 6 = vkey 0x36
   #win_sendinput.KeyInputSend_vkey(win_sendinput.KEYEVENTF_KEYDOWN,0x36)
   #win_sendinput.KeyInputSend_vkey(win_sendinput.KEYEVENTF_KEYUP,0x36)

   # Scan Key - Key 6 = Scan code 7
   #win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYDOWN,7)
   #win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYUP,7)

   # Scan Key - Key 1 = Scan code 2
   win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYDOWN,2)
   win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYUP,2)

if __name__ == '__main__':
   main()


Link to comment
Share on other sites

For Freddy,

 


#-------------------------------------------------------------------------------
# Name:        os_win_sendinput
# Purpose:
#
# Author:      Fred
#
# Created:     08/11/2012
# Copyright:   (c) Fred 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

#-------------------------------------------------------------------------------
# import
#-------------------------------------------------------------------------------
from ctypes import *
user32 = windll.user32

#-------------------------------------------------------------------------------
# import for main
#-------------------------------------------------------------------------------
import time

#-------------------------------------------------------------------------------
#
#-------------------------------------------------------------------------------
# START SENDINPUT TYPE DECLARATIONS
PUL = POINTER(c_ulong)

class KeyBdInput(Structure):
   _fields_ = [("wVk", c_ushort),
            ("wScan", c_ushort),
            ("dwFlags", c_ulong),
            ("time", c_ulong),
            ("dwExtraInfo", PUL)]

class HardwareInput(Structure):
   _fields_ = [("uMsg", c_ulong),
            ("wParamL", c_short),
            ("wParamH", c_ushort)]

class MouseInput(Structure):
   _fields_ = [("dx", c_long),
            ("dy", c_long),
            ("mouseData", c_ulong),
            ("dwFlags", c_ulong),
            ("time",c_ulong),
            ("dwExtraInfo", PUL)]

class Input_I(Union):
   _fields_ = [("ki", KeyBdInput),
             ("mi", MouseInput),
             ("hi", HardwareInput)]

class Input(Structure):
   _fields_ = [("type", c_ulong),
            ("ii", Input_I)]

# END SENDINPUT TYPE DECLARATIONS

#-------------------------------------------------------------------------------
# Class
#-------------------------------------------------------------------------------
class win_sendinput:
   """
   win_sendinput is a class for use of the function sendinput.
   """

   #---------------------------------------------------------------------------
   # Class constants
   #---------------------------------------------------------------------------
   INPUT_MOUSE = 0
   INPUT_KEYBOARD = 1
   INPUT_HARDWARE = 2

   KEYEVENTF_KEYDOWN = 0
   KEYEVENTF_KEYUP = 2
   KEYEVENTF_SCANCODE = 8

   MOUSEEVENTF_MOVE = 0x0001
   MOUSEEVENTF_LEFTDOWN = 0x0002
   MOUSEEVENTF_LEFTUP = 0x0004
   MOUSEEVENTF_RIGHTDOWN = 0x0008
   MOUSEEVENTF_RIGHTUP = 0x0010
   MOUSEEVENTF_MIDDLEDOWN = 0x0020
   MOUSEEVENTF_MIDDLEUP = 0x0040
   MOUSEEVENTF_ABSOLUTE = 0x8000
   #---------------------------------------------------------------------------
   # Mouse primitive functions for windows-
   #---------------------------------------------------------------------------
   @staticmethod
   def MouseInputSend(Flag,dx,dy):
       """
       """
       extra = c_ulong(0)
       Inp = Input()
       Inp.type = win_sendinput.INPUT_MOUSE
       Inp.ii.mi = MouseInput(dx, dy, 0, Flag, 0, pointer(extra))
       Size = sizeof(Inp)
       user32.SendInput(1, pointer(Inp), sizeof(Inp))

   @staticmethod
   def KeyInputSend_vkey(Flag, Key):
       """
       """
       Inp = Input()
       Inp.type = win_sendinput.INPUT_KEYBOARD
       Inp.ii.ki.dwFlags = Flag
       Inp.ii.ki.wVk = Key
       Size = sizeof(Inp)
       user32.SendInput(1, pointer(Inp), sizeof(Inp))


   @staticmethod
   def KeyInputSend_ScanCode(Flag, Key):
       """
       """
       Inp = Input()
       Inp.type = win_sendinput.INPUT_KEYBOARD
       Inp.ii.ki.dwFlags = win_sendinput.KEYEVENTF_SCANCODE | Flag
       Inp.ii.ki.wScan = Key
       Size = sizeof(Inp)
       user32.SendInput(1, pointer(Inp), sizeof(Inp))



#-------------------------------------------------------------------------------
# TEST
#-------------------------------------------------------------------------------
def main():
   time.sleep(5.0)
   # Virtual Key - Key 6 = vkey 0x36
   #win_sendinput.KeyInputSend_vkey(win_sendinput.KEYEVENTF_KEYDOWN,0x36)
   #win_sendinput.KeyInputSend_vkey(win_sendinput.KEYEVENTF_KEYUP,0x36)

   # Scan Key - Key 6 = Scan code 7
   #win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYDOWN,7)
   #win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYUP,7)

   # Scan Key - Key 1 = Scan code 2
   win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYDOWN,2)
   win_sendinput.KeyInputSend_ScanCode(win_sendinput.KEYEVENTF_KEYUP,2)

if __name__ == '__main__':
   main()


 

The only difference I see between this and SCAR's KeyDown/KeyUp functions is that it uses KEYEVENTF_SCANCODE rather than KEYEVENTF_UNICODE. I will disable unicode by default and add an option to enable this to TSCARWindowClient, so it will use KEYEVENTF_SCANCODE by default. If that doesn't resolve the issue, I don't know what will.

Link to comment
Share on other sites

EDIT: Actually, I think I'm going to add a mode to send VKeys as scancodes instead, that is much more convenient in the system that is currently in place.

 

EDIT2: You can now toggle on the use of scancodes with [wiki=TSCARWindowClient.Scancodes]TSCARWindowClient.Scancodes[/wiki]. I suggest you give that a try when I release 3.37 RC later today.

Edited by Freddy
Link to comment
Share on other sites

That completes the regular keyboard functions. The silent ones would be with using post message, or some method.

 

From my research I have concluded that in order to get around games with security such as XTrap, GameGuard, Punk Puster, and etc. You would need to make your own keyboard driver, and make it so that it works on win xp ~ win 8. This is the 100% way to do it. Otherwise the games can hook certain functions, and prevent you from doing things. They will directly hook the keyboard at a really low level or DirectInput. Then you can't send input to the window, or use post message, it just won't do anything because the keyboard device/driver is being interfaced directly. They are not using any old message they get from send input or post message, or they are blocked totally.

 

Its odd though to me that Mouse functions for SCAR work perfectly on most games. Why is mouse input different here? Isn't it faked too if it is sent using SendInput? How does that work...

Link to comment
Share on other sites

That completes the regular keyboard functions. The silent ones would be with using post message, or some method.

There's no other method than sending messages...

 

From my research I have concluded that in order to get around games with security such as XTrap, GameGuard, Punk Puster, and etc. You would need to make your own keyboard driver, and make it so that it works on win xp ~ win 8. This is the 100% way to do it. Otherwise the games can hook certain functions, and prevent you from doing things. They will directly hook the keyboard at a really low level or DirectInput. Then you can't send input to the window, or use post message, it just won't do anything because the keyboard device/driver is being interfaced directly. They are not using any old message they get from send input or post message, or they are blocked totally.

Windows sends a flag along with input messages that indicates whether it came from an API or a driver, this makes it easy for programs to distinguish between the two.

 

Its odd though to me that Mouse functions for SCAR work perfectly on most games. Why is mouse input different here? Isn't it faked too if it is sent using SendInput? How does that work...

I'm not sure why, but I'm guessing that without a keyboard, most of the games that block fake input can be controlled properly.

Link to comment
Share on other sites

Freddy

 

When you say , 'Windows sends a flag along with input messages that indicates whether it came from an API or a driver, this makes it easy for programs to distinguish between the two.' . Could you explain and show us an example or a link ?

 

For LordJashin

Yes it is possible to send message in silent mode , but you need to take care because your window game is not on the activated window (foreground). And the Game can detect if his window is the foreground or not.

Link to comment
Share on other sites

HI Freddy

 

With Scar 3.37 and 'TSCARWindowClient(GetClient).Scancodes := True;

 

- The vKeyDown and vKeyUp work fine

 

- Is it the same for KeyUp and KeyDown ? I tried but it doesn t work.

 

Thanks

No, they still work the same as before, sending unicode characters. I initially intended to change those rather than the VKey functions, but doing so would break or change a lot of existing functionality.

Link to comment
Share on other sites

What I don't understand is. Where would you send the messages if the Game uses an API for getting the messages from the keyboard such as Direct Input or XInput? Would you just send them to the window as usual but now you need that flag set to either API or driver. Like...on a chain, how does it go:

 

Keyboard->Driver->Kernal???->Windows System->API or Windows API???->Game/Application

 

I guess my main issue here, is how the messages go around....

Edited by LordJashin
Link to comment
Share on other sites

What I don't understand is. Where would you send the messages if the Game uses an API for getting the messages from the keyboard such as Direct Input or XInput? Would you just send them to the window as usual but now you need that flag set to either API or driver. Like...on a chain, how does it go:

 

Keyboard->Driver->Kernal???->Windows System->API or Windows API???->Game/Application

 

I guess my main issue here, is how the messages go around....

 

I have no idea what you're talking about... Also, the kernel is part of windows...

 

The driver is essentially just a bridge between the device and the kernel, as the kernel by itself doesn't know how a keyboard works. Keyboard events are sent asynchronously to the operating system. When this happens an interrupt will push the OS from user mode into kernel mode where the event will be processed. The kernel will then generate a window message that's sent to the foreground window, after which you re-enter user mode. The kernel does also offers methods to get key presses asynchronously, as demonstrated by GetAsyncKeyState. As I mentioned, the OS also stores a flag indicating whether the input came from a driver. As far as I'm aware, this flag isn't sent along with the key in windowmessages, but it is possible to retrieve it through the window API, using, among other things, keyboard hooks. And note that applications can also use keyboard hooks to get keyboard input, rather than relying on window messages.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...