Python win32api.keybd_event() Examples

The following are 8 code examples of win32api.keybd_event(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module win32api , or try the search function .
Example #1
Source File: game.py    From twitch-plays with MIT License 6 votes vote down vote up
def push_button(self, button):
        win32api.keybd_event(self.button_to_key(button), 0, 0, 0)
        time.sleep(.15)
        win32api.keybd_event(self.button_to_key(button), 0, win32con.KEYEVENTF_KEYUP, 0) 
Example #2
Source File: windows.py    From airtest with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def type(self, text):
        ''' Type text into device '''
        
        for c in text:
            if c in ShiftCodes:  #judge the character is a capital letter or not; 16 is the value of shift
                win32api.keybd_event(16,win32api.MapVirtualKey(16,0),0,0)
                win32api.keybd_event(ShiftCodes[c],win32api.MapVirtualKey(ShiftCodes[c],0),0,0)
                win32api.keybd_event(16,win32api.MapVirtualKey(16,0),win32con.KEYEVENTF_KEYUP,0)
                win32api.keybd_event(ShiftCodes[c],win32api.MapVirtualKey(ShiftCodes[c],0),win32con.KEYEVENTF_KEYUP,0)
            elif c in OriginalCodes:    #judge the character is a capital letter or not
                win32api.keybd_event(OriginalCodes[c],win32api.MapVirtualKey(OriginalCodes[c],0),0,0)
                win32api.keybd_event(OriginalCodes[c],win32api.MapVirtualKey(OriginalCodes[c],0),win32con.KEYEVENTF_KEYUP,0) 
Example #3
Source File: winguiauto.py    From pyAutoTrading with GNU General Public License v2.0 5 votes vote down vote up
def sendKeyEvent(key, command):
    win32api.keybd_event(key, 0, command, 0)
    time.sleep(0.2) 
Example #4
Source File: code_editor.py    From equant with GNU General Public License v2.0 5 votes vote down vote up
def do_key_event(self, keys, is_group):
        for key in keys:
            win32api.keybd_event(int(key), 0, 0, 0)
            if not is_group:
                win32api.keybd_event(int(key), 0, win32con.KEYEVENTF_KEYUP, 0)

        if not is_group:
            return
        keys.reverse()
        for key in keys:
            win32api.keybd_event(int(key), 0, win32con.KEYEVENTF_KEYUP, 0)


# 常规函数 
Example #5
Source File: media_controls.py    From AndroidMediaControlsWindows with GNU General Public License v3.0 5 votes vote down vote up
def toggle_play():
    win32api.keybd_event(VK_MEDIA_PLAY_PAUSE, 0, 0, 0) 
Example #6
Source File: sampler.py    From DeepWarp with Apache License 2.0 4 votes vote down vote up
def sample_one_person(n, num_x=5, num_y=5):
    save_path = 'D:/UnityEyes_Windows/imgs'
    if os.path.exists(save_path) == False:
        os.mkdir(save_path)

    # reset
    win32gui.SendMessage(handle, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0)
    center_x = (clt_left + clt_right) // 2
    center_y = (clt_top + clt_bottom) // 2
    win32api.SetCursorPos([center_x, center_y])

    # press 'L'
    win32api.keybd_event(KEY_LIGHT, 0, 0, 0)  # key down
    time.sleep(1)
    win32api.keybd_event(KEY_LIGHT, 0, win32con.KEYEVENTF_KEYUP, 0)  # key up
    # press 'R'
    win32api.keybd_event(KEY_RANDOM, 0, 0, 0)  # key down
    time.sleep(1)
    win32api.keybd_event(KEY_RANDOM, 0, win32con.KEYEVENTF_KEYUP, 0)  # key up

    # number of points for vertical and horizontal
    # num_x, num_y = 5, 5

    step_x, step_y = width // (num_x + 1), height // (num_y + 1)
    for i in range(1, num_y+1):
        for j in range(1, num_x+1):
            x = clt_left + j * step_x
            y = clt_top + i * step_y
            print('{},{}'.format(x, y))
            win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
            win32api.SetCursorPos([x, y])
            win32api.mouse_event(win32con.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
            time.sleep(0.5)
            win32api.keybd_event(KEY_SAVE, 0, 0, 0) # key down
            win32api.keybd_event(KEY_SAVE, 0, win32con.KEYEVENTF_KEYUP, 0)  # key up 
Example #7
Source File: studiomaxscene.py    From cross3d with MIT License 4 votes vote down vote up
def importFBX(self, path, showUI=False, **kwargs):

		# Setting up presets.
		presetPaths = self._fbxIOPresetPaths(action='import')
		templatePath = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'fbx_import_preset.pytempl'))

		# Setting up the FBX presets.
		self._setupFBXIOPresets(presetPaths, templatePath, **{'user': getpass.getuser()})

		# We always show UI in debug mode.
		showUI = True if cross3d.debugLevel >= cross3d.constants.DebugLevels.Mid else showUI

		# If the preset has been modified since the last export, we make sure to reload ours by showing the UI.
		if showUI or (presetPaths and os.path.getmtime(presetPaths[0]) > self._fbxIOPresetModifiedTime + 100):

			# If the user did not want to see the UI, we prepare some callbacks that will press the enter key for him.
			if not showUI:

				# Creating a method that presses enter.
				def pressEnter():
					win32api.keybd_event(0x0D, 0x0D, 0, 0)
					win32api.keybd_event(0x0D, 0x0D, win32con.KEYEVENTF_KEYUP, 0)

				# There will be a prompt for the FBX options.
				QTimer.singleShot(200, pressEnter)

				# There might be a second prompt if the file needs to be overwritten.
				if os.path.exists(path):
					QTimer.singleShot(400, pressEnter)

			# Exporting showin the UI.
			mxs.importfile(path)

		else:
			# Calling the FBX exporter without GUI.
			mxs.importfile(path, mxs.pyhelper.namify("noPrompt"))

		# Restoring presets.
		self._restoreFBXIOPresets()

		# TODO: Softimage returns a model. Here we return a boolean. Do we want to make imported FBX into models or maybe return a list of objects?
		return True 
Example #8
Source File: Email My PC.py    From Email_My_PC with MIT License 4 votes vote down vote up
def button_event(content):
	key_table = {'BACKSPACE':8, 'TAB':9, 'TABLE':9, 'CLEAR':12, 'ENTER':13, 'SHIFT':16, 'CTRL':17, 
		'CONTROL':17, 'ALT':18, 'ALTER':18, 'PAUSE':19, 'BREAK':19, 'CAPSLK':20, 'CAPSLOCK':20, 'ESC':27, 
		'SPACE':32, 'SPACEBAR':32, 'PGUP':33, 'PAGEUP':33, 'PGDN':34, 'PAGEDOWN':34, 'END':35, 'HOME':36, 
		'LEFT':37, 'UP':38, 'RIGHT':39, 'DOWN':40, 'SELECT':41, 'PRTSC':42, 'PRINTSCREEN':42, 'SYSRQ':42, 
		'SYSTEMREQUEST':42, 'EXECUTE':43, 'SNAPSHOT':44, 'INSERT':45, 'DELETE':46, 'HELP':47, 'WIN':91, 
		'WINDOWS':91, 'F1':112, 'F2':113, 'F3':114, 'F4':115, 'F5':116, 'F6':117, 'F7':118, 'F8':119, 
		'F9':120, 'F10':121, 'F11':122, 'F12':123, 'F13':124, 'F14':125, 'F15':126, 'F16':127, 'NMLK':144, 
		'NUMLK':144, 'NUMLOCK':144, 'SCRLK':145, 'SCROLLLOCK':145, 'LEFTCLICK':999, 'RIGHTCLICK':1000}
	unrecognized = ''
	key_values = []
	keys = content.split('+')
	for key in keys:
		raw_key = key
		key = key.strip().replace(' ','').upper()
		if key in key_table:
			key_values.append(key_table.get(key))
		elif len(key) == 1:
			key_values.append(ord(key))
		else:
			if key != '':
				unrecognized = raw_key
	for key_value in key_values:
		if key_value == 999:
			win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
		elif key_value == 1000:
			win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
		else:
			win32api.keybd_event(key_value, 0, 0, 0)
		time.sleep(1)
	for i in range(len(key_values)-1, -1, -1):
		if key_value == 999:
			win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
		elif key_value == 1000:
			win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
		else:
			win32api.keybd_event(key_values[i], 0, win32con.KEYEVENTF_KEYUP, 0)
		time.sleep(1)
	return unrecognized

#设置开机启动快捷方式