查看: 2227|回复: 4

[已经解决] 按下鼠标左键应该怎么写代码?

[复制链接]
梦石
0
星屑
50
在线时间
207 小时
注册时间
2014-8-16
回帖
62
发表于 2015-5-29 14:50:51 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
按下鼠标左键应该怎么希望代码?if Input.trigger?(Input::C)我想把它修改成鼠标的左键,应该怎么写,都有几种写法.,谢谢!

评分

参与人数 1星屑 +35 收起 理由
RyanBern + 35 手动认可奖励

查看全部评分

梦石
0
星屑
1232
在线时间
1017 小时
注册时间
2011-4-30
回帖
1464
发表于 2015-5-29 17:26:35 | 显示全部楼层
主要是调用win32。下面这个大概是某位大神的脚本,不记得谁了。

  1. GetAsyncKeyState = Win32API.new("user32","GetAsyncKeyState",['I'],'I')
  2. #1、Kboard.press?(key) 同 Input.press?(key)
  3. #2、Kboard.repeat?(key) 同 Input.repeat?(key)
  4. #3、Kboard.trigger?(key) 同 Input.trigger?(key)
  5. module Kboard
  6.         #==========================================================================
  7.         # 以下是全键盘按键列表
  8.         #--------------------------------------------------------------------------
  9.         M_L = 0x01        # left mouse button
  10.         M_R = 0x02        # right mouse button

  11.         @R_Key_Hash = {}
  12.         @R_Key_Repeat = {}
  13.        
  14.         def self.press?(rkey)
  15.                 return GetAsyncKeyState.call(rkey) != 0
  16.         end
  17.        
  18.         def self.repeat?(rkey)
  19.                 result = GetAsyncKeyState.call(rkey)
  20.                 if result != 0
  21.                         if @R_Key_Repeat[rkey].nil?
  22.                                 @R_Key_Repeat[rkey] = 0
  23.                                 return true
  24.                         end
  25.                         @R_Key_Repeat[rkey] += 1
  26.                 else
  27.                         @R_Key_Repeat[rkey] = nil
  28.                         @R_Key_Hash[rkey] = 0
  29.                 end
  30.                 if !@R_Key_Repeat[rkey].nil? and @R_Key_Repeat[rkey] > 4 # 4乃准确数字
  31.                         @R_Key_Repeat[rkey] = 0
  32.                         return true
  33.                 else
  34.                         return false
  35.                 end
  36.         end
  37.        
  38.         def self.trigger?(rkey)
  39.                 result = GetAsyncKeyState.call(rkey)
  40.                 if @R_Key_Hash[rkey] == 1 and result != 0
  41.                         return false
  42.                 end
  43.                 if result != 0
  44.                         @R_Key_Hash[rkey] = 1
  45.                         return true
  46.                 else
  47.                         @R_Key_Hash[rkey] = 0
  48.                         return false
  49.                 end
  50.         end
  51. end

复制代码
回复

使用道具 举报

梦石
0
星屑
76
在线时间
1379 小时
注册时间
2012-7-5
回帖
1643

开拓者

发表于 2015-5-30 19:00:40 | 显示全部楼层
本帖最后由 kuerlulu 于 2015-5-30 19:19 编辑

自己写的鼠标脚本:
  1. def _api code; Win32API.new *code.split("|"); end

  2. #==============================================================================
  3. # ■ Mouse
  4. #------------------------------------------------------------------------------
  5. #   处理鼠标输入信息的模块。
  6. #==============================================================================
  7. module Mouse
  8.   #--------------------------------------------------------------------------
  9.   # ● 监视器
  10.   #--------------------------------------------------------------------------
  11.   Monitor = {
  12.     :pos         => [0, 0],
  13.     :last_pos    => [0, 0],
  14.     :save_pos    => [0, 0],
  15.     :button      => 0,
  16.     :last_button => 0,
  17.     :timer       => 0,
  18.   }
  19.   Dragage = {}
  20.   #--------------------------------------------------------------------------
  21.   # ● API
  22.   #--------------------------------------------------------------------------
  23.   GetCursorPos     = _api "user32|GetCursorPos|p|i"
  24.   ScreenToClient   = _api "user32|ScreenToClient|pp|i"
  25.   GetActiveWindow  = _api "user32|GetActiveWindow||i"
  26.   GetAsyncKeyState = _api "user32|GetAsyncKeyState|i|i"
  27.   ShowCursor       = _api "user32|ShowCursor|i|i"
  28. class << self
  29.   def update
  30.     # ● 更新监视器
  31.     Monitor[:last_pos] = Monitor[:pos]
  32.     Monitor[:pos] = _pos
  33.     Monitor[:last_button] = Monitor[:button]
  34.     Monitor[:button] = (_press? 0x01) ? 1 : (_press? 0x02) ? 2 : (
  35.       _press?(0x04) ? 3 : 0 )
  36.     Monitor[:last_button] == Monitor[:button] ? Monitor[:timer] += 1 : (
  37.       Monitor[:timer] = 0 )
  38.     # ● 更新拖动
  39.     Monitor[:save_pos] = Monitor[:pos] if down_left?
  40.     Dragage.clear if Monitor[:button] == 0 && !Dragage.empty?
  41.   end
  42.   #--------------------------------------------------------------------------
  43.   def _press? button
  44.     GetAsyncKeyState.call(button).abs & 0x8000 == 0x8000
  45.   end
  46.   def _pos
  47.     GetCursorPos.call buffer = "\0"*8
  48.     ScreenToClient.call GetActiveWindow.call, buffer
  49.     buffer.unpack "ll"
  50.   end
  51.   #--------------------------------------------------------------------------
  52.   # ● 快速找到正确的姿势
  53.   #--------------------------------------------------------------------------
  54.   def pos; Monitor[:pos]; end
  55.   def button; Monitor[:button]; end
  56.   def moving?; Monitor[:timer] == 0; end
  57.   def down_left?; Monitor[:button] == 1 && Monitor[:last_button] == 0; end
  58.   def down_right?; Monitor[:button] == 2 && Monitor[:last_button] == 0; end
  59.   def hold_left?; Monitor[:button] == 1 && Monitor[:last_button] == 1; end
  60.   def hold_right?; Monitor[:button] == 2 && Monitor[:last_button] == 2; end
  61.   def up_left?; Monitor[:button] == 0 && Monitor[:last_button] == 1; end
  62.   def up_right?; Monitor[:button] == 0 && Monitor[:last_button] == 2; end
  63.   def hold?; hold_left? || hold_right?; end
  64.   alias click? down_left?
  65.   alias right_click? down_right?
  66.   def add(obj); Dragage[obj] = [obj.x, obj.y, obj.width, obj.height]; end
  67.   def hide; ShowCursor.call(0); end
  68.   def show; ShowCursor.call(1); end
  69.   #--------------------------------------------------------------------------
  70.   def area?(*args)
  71.     case args.size
  72.     when 1 then rect?(args[0].x, args[0].y, args[0].width, args[0].height)
  73.     when 4 then rect?(*args)
  74.     end
  75.   end
  76.   def rect?(x, y, width, height)
  77.     Monitor[:pos][0].between?(x, x + width) &&
  78.     Monitor[:pos][1].between?(y, y + height)
  79.   end
  80.   def [](arg)
  81.     return Monitor[arg] if arg.is_a? Symbol
  82.     Dragage[arg]
  83.   end
  84.   def has?(obj)
  85.     Dragage.keys.include?(obj)
  86.   end
  87.   private :_press?, :_pos
  88. end
  89. end

  90. class << Input
  91.   alias hy_update update
  92.   def update
  93.     hy_update
  94.     Mouse.update
  95.   end
  96. end
复制代码
调用姿势: Mouse.click?

评分

参与人数 1星屑 +200 收起 理由
RyanBern + 200 认可答案

查看全部评分


  -fk: -azogi:
回复

使用道具 举报

梦石
0
星屑
50
在线时间
207 小时
注册时间
2014-8-16
回帖
62
 楼主| 发表于 2015-5-30 20:48:42 | 显示全部楼层
kuerlulu 发表于 2015-5-30 19:00
自己写的鼠标脚本:调用姿势: Mouse.click?


如果调用鼠标左键,该怎么写?谢谢!

点评

Win32API.new("user32", "keybd_event", "iiii", 'l').call(0x01)  发表于 2015-5-30 21:04
回复

使用道具 举报

梦石
0
星屑
1748
在线时间
485 小时
注册时间
2006-1-7
回帖
996
发表于 2015-5-30 21:18:39 | 显示全部楼层
给你一个全键盘脚本,这样键盘上所有按钮你都可以调用了。
我个人还添加了注解,因为原来作者的注解实在蛋疼

我觉得注解内容你应该看得懂
按一下鼠标左键
就是  if Kboard.keyboard($Rmouse_BUTTON_L)


全键盘脚本
  1. #============================================================================
  2. # ■ Keyboard Script
  3. #----------------------------------------------------------------------------
  4. #  By: Cybersam
  5. #   Date: 25/05/05
  6. #   Version 4
  7. #============================================================================
  8. =begin
  9. 以下是 白鬼 的注解

  10. ★按一下就成立
  11. if Kboard.keyboard($R_Key_1)#按下1就会执行

  12. end

  13. ★按住就会成立
  14. if Kboard.keyb($R_Key_2) == 1#连按住2就会成立并执行

  15. end

  16. ★默认是true,按一下切换成false
  17. if Kboard.key($R_Key_3) == true  #默认是true,按一下切换成false
  18.   S1#不按就在执行S1
  19. else
  20.   S2#按下一次就切换成执行S2,再按再切回去
  21. end

  22. =end
  23. #============================================================================

  24. module Kboard
  25.   #==========================================================================
  26.   # 以下是全键盘按键列表
  27.   #--------------------------------------------------------------------------
  28.   $Rmouse_BUTTON_L = 0x01        # left mouse button
  29.   $Rmouse_BUTTON_R = 0x02        # right mouse button
  30.   $Rmouse_BUTTON_M = 0x04        # middle mouse button
  31.   $Rmouse_BUTTON_4 = 0x05        # 4th mouse button
  32.   $Rmouse_BUTTON_5 = 0x06        # 5th mouse button
  33.   #--------------------------------------------------------------------------
  34.   $R_Key_BACK      = 0x08        # BACKSPACE key
  35.   $R_Key_TAB       = 0x09        # TAB key
  36.   $R_Key_RETURN    = 0x0D        # ENTER key
  37.   $R_Key_SHIFT     = 0x10        # SHIFT key
  38.   $R_Key_CTLR      = 0x11        # CTLR key
  39.   $R_Key_ALT       = 0x12        # ALT key
  40.   $R_Key_PAUSE     = 0x13        # PAUSE key
  41.   $R_Key_CAPITAL   = 0x14        # CAPS LOCK key
  42.   $R_Key_ESCAPE    = 0x1B        # ESC key
  43.   $R_Key_SPACE     = 0x20        # SPACEBAR
  44.   $R_Key_PRIOR     = 0x21        # PAGE UP key
  45.   $R_Key_NEXT      = 0x22        # PAGE DOWN key
  46.   $R_Key_END       = 0x23        # END key
  47.   $R_Key_HOME      = 0x24        # HOME key
  48.   $R_Key_LEFT      = 0x25        # LEFT ARROW key
  49.   $R_Key_UP        = 0x26        # UP ARROW key
  50.   $R_Key_RIGHT     = 0x27        # RIGHT ARROW key
  51.   $R_Key_DOWN      = 0x28        # DOWN ARROW key
  52.   $R_Key_SELECT    = 0x29        # SELECT key
  53.   $R_Key_PRINT     = 0x2A        # PRINT key
  54.   $R_Key_SNAPSHOT  = 0x2C        # PRINT SCREEN key
  55.   $R_Key_INSERT    = 0x2D        # INS key
  56.   $R_Key_DELETE    = 0x2E        # DEL key
  57.   #--------------------------------------------------------------------------
  58.   $R_Key_0         = 0x30        # 0 key
  59.   $R_Key_1         = 0x31        # 1 key
  60.   $R_Key_2         = 0x32        # 2 key
  61.   $R_Key_3         = 0x33        # 3 key
  62.   $R_Key_4         = 0x34        # 4 key
  63.   $R_Key_5         = 0x35        # 5 key
  64.   $R_Key_6         = 0x36        # 6 key
  65.   $R_Key_7         = 0x37        # 7 key
  66.   $R_Key_8         = 0x38        # 8 key
  67.   $R_Key_9         = 0x39        # 9 key
  68.   #--------------------------------------------------------------------------
  69.   $R_Key_A         = 0x41        # A key
  70.   $R_Key_B         = 0x42        # B key
  71.   $R_Key_C         = 0x43        # C key
  72.   $R_Key_D         = 0x44        # D key
  73.   $R_Key_E         = 0x45        # E key
  74.   $R_Key_F         = 0x46        # F key
  75.   $R_Key_G         = 0x47        # G key
  76.   $R_Key_H         = 0x48        # H key
  77.   $R_Key_I         = 0x49        # I key
  78.   $R_Key_J         = 0x4A        # J key
  79.   $R_Key_K         = 0x4B        # K key
  80.   $R_Key_L         = 0x4C        # L key
  81.   $R_Key_M         = 0x4D        # M key
  82.   $R_Key_N         = 0x4E        # N key
  83.   $R_Key_O         = 0x4F        # O key
  84.   $R_Key_P         = 0x50        # P key
  85.   $R_Key_Q         = 0x51        # Q key
  86.   $R_Key_R         = 0x52        # R key
  87.   $R_Key_S         = 0x53        # S key
  88.   $R_Key_T         = 0x54        # T key
  89.   $R_Key_U         = 0x55        # U key
  90.   $R_Key_V         = 0x56        # V key
  91.   $R_Key_W         = 0x57        # W key
  92.   $R_Key_X         = 0x58        # X key
  93.   $R_Key_Y         = 0x59        # Y key
  94.   $R_Key_Z         = 0x5A        # Z key
  95.   #--------------------------------------------------------------------------
  96.   $R_Key_LWIN      = 0x5B        # Left Windows key (Microsoft Natural keyboard)
  97.   $R_Key_RWIN      = 0x5C        # Right Windows key (Natural keyboard)
  98.   $R_Key_APPS      = 0x5D        # Applications key (Natural keyboard)
  99.   #--------------------------------------------------------------------------
  100.   $R_Key_NUMPAD0   = 0x60        # Numeric keypad 0 key
  101.   $R_Key_NUMPAD1   = 0x61        # Numeric keypad 1 key
  102.   $R_Key_NUMPAD2   = 0x62        # Numeric keypad 2 key
  103.   $R_Key_NUMPAD3   = 0x63        # Numeric keypad 3 key
  104.   $R_Key_NUMPAD4   = 0x64        # Numeric keypad 4 key
  105.   $R_Key_NUMPAD5   = 0x65        # Numeric keypad 5 key
  106.   $R_Key_NUMPAD6   = 0x66        # Numeric keypad 6 key
  107.   $R_Key_NUMPAD7   = 0x67        # Numeric keypad 7 key
  108.   $R_Key_NUMPAD8   = 0x68        # Numeric keypad 8 key
  109.   $R_Key_NUMPAD9  = 0x69        # Numeric keypad 9 key
  110.   $R_Key_MULTIPLY  = 0x6A        # Multiply key (*)
  111.   $R_Key_ADD       = 0x6B        # Add key (+)
  112.   $R_Key_SEPARATOR = 0x6C        # Separator key
  113.   $R_Key_SUBTRACT  = 0x6D        # Subtract key (-)
  114.   $R_Key_DECIMAL   = 0x6E        # Decimal key
  115.   $R_Key_DIVIDE    = 0x6F        # Divide key (/)
  116.   #--------------------------------------------------------------------------
  117.   $R_Key_F1        = 0x70        # F1 key
  118.   $R_Key_F2        = 0x71        # F2 key
  119.   $R_Key_F3        = 0x72        # F3 key
  120.   $R_Key_F4        = 0x73        # F4 key
  121.   $R_Key_F5        = 0x74        # F5 key
  122.   $R_Key_F6        = 0x75        # F6 key
  123.   $R_Key_F7        = 0x76        # F7 key
  124.   $R_Key_F8        = 0x77        # F8 key
  125.   $R_Key_F9        = 0x78        # F9 key
  126.   $R_Key_F10       = 0x79        # F10 key
  127.   $R_Key_F11       = 0x7A        # F11 key
  128.   $R_Key_F12       = 0x7B        # F12 key
  129.   #--------------------------------------------------------------------------
  130.   $R_Key_NUMLOCK   = 0x90        # NUM LOCK key
  131.   $R_Key_SCROLL    = 0x91        # SCROLL LOCK key
  132.   #--------------------------------------------------------------------------
  133.   $R_Key_LSHIFT    = 0xA0        # Left SHIFT key
  134.   $R_Key_RSHIFT    = 0xA1        # Right SHIFT key
  135.   $R_Key_LCONTROL  = 0xA2        # Left CONTROL key
  136.   $R_Key_RCONTROL  = 0xA3        # Right CONTROL key
  137.   $R_Key_L_ALT    = 0xA4        # Left ALT key
  138.   $R_Key_R_ALT    = 0xA5        # Right ALT key
  139.   #--------------------------------------------------------------------------
  140.   $R_Key_SEP      = 0xBC        # , key
  141.   $R_Key_DASH      = 0xBD        # - key
  142.   $R_Key_DOTT      = 0xBE        # . Key
  143.   #--------------------------------------------------------------------------
  144.   GetKeyState = Win32API.new("user32","GetAsyncKeyState",['I'],'I')
  145.   GetKeyboardState = Win32API.new("user32","GetKeyState",['I'],'I')
  146.   GetSetKeyState = Win32API.new("user32","SetKeyboardState",['I'],'I')
  147.   #--------------------------------------------------------------------------
  148.   module_function
  149.   #--------------------------------------------------------------------------
  150.   def keyb(rkey)
  151.      if GetKeyState.call(rkey) != 0
  152.        return 1
  153.      end
  154.      return 0
  155.   end
  156.    #--------------------------------------------------------------------------
  157.   def keyboard(rkey)
  158.     GetKeyState.call(rkey) & 0x01 == 1#
  159.   end
  160.   #--------------------------------------------------------------------------
  161.   def key(rkey, key = 0)
  162.     GetKeyboardState.call(rkey) & 0x01 == key #
  163.   end
  164. end
复制代码
初从文,三年不中;后习武,校场发一矢,中鼓吏,逐之出;遂学医,有所成。自撰一良方,服之,卒。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部