vb.net - Issue toggling caps lock with check box in vb .net -


i've developed application (with vb .net) can toggle caps lock state clicking on check box. i've coded program in such way when click on checkbox, if gets checked caps lock must turned on , when unchecked must turn off. below codes.

public class form1  private declare sub keybd_event lib "user32" (byval bvk byte, byval bscan byte, byval dwflags integer, byval dwextrainfo integer)  private const vk_capital integer = &h14 private const vk_scroll integer = &h91 private const vk_numlock integer = &h90  private const keyeventf_extendedkey integer = &h1 private const keyeventf_keyup integer = &h2  private sub checkbutton_caps_checkedchanged(sender object, e eventargs) handles checkbutton_caps.checkstatechanged      if checkbutton_caps.checked = true         call keybd_event(vk_capital, &h45, keyeventf_extendedkey or 0, 0)         checkbutton_caps.image = image.fromfile("resources\btn_ico_caps_on.png")      elseif checkbutton_caps.checked = false         call keybd_event(vk_capital, &h45, keyeventf_extendedkey or keyeventf_keyup, 0)         checkbutton_caps.image = image.fromfile("resources\btn_ico_caps_off.png")      end if end sub end class 

now problem it's not working expected. if check checkbox, image of checkbox changes not caps lock status. caps lock status changes when click on check box twice. need click on check box twice toggle caps lock. suspect there's problem in way i've used conditional statements.

the problem not conditional logic per se (in other words, you've written if statement correctly), rather way using keybd_event function.

think physical keys on keyboard. when "press" key, 2 things happen: key goes down, , key comes up. after both of things happen, key press event registered having taken place.

so proper way trigger key press using keybd_event function inject key-down event, followed key-up event. need pair of calls:

 call keybd_event(vk_capital, &h45, keyeventf_extendedkey or 0, 0)  call keybd_event(vk_capital, &h45, keyeventf_extendedkey or keyeventf_keyup, 0) 

now understand that, @ code. checkbox control starts out unchecked. first time click on it, gets automatically checked , checkedchanged event handler fires. in response, make first call keybd_event, "presses down" caps-lock key. not until second time click checkbox control, switching back unchecked , making second call keybd_event trigger full press of caps lock key.

write code so:

private sub checkbutton_caps_checkedchanged(sender object, e eventargs) handles checkbutton_caps.checkstatechanged      if checkbutton_caps.checked = true         call keybd_event(vk_capital, &h45, keyeventf_extendedkey or 0, 0)         call keybd_event(vk_capital, &h45, keyeventf_extendedkey or keyeventf_keyup, 0)         checkbutton_caps.image = image.fromfile("resources\btn_ico_caps_on.png")      elseif checkbutton_caps.checked = false         call keybd_event(vk_capital, &h45, keyeventf_extendedkey or 0, 0)         call keybd_event(vk_capital, &h45, keyeventf_extendedkey or keyeventf_keyup, 0)         checkbutton_caps.image = image.fromfile("resources\btn_ico_caps_off.png")      end if  end sub 

or, better yet, encapsulate logic trigger key press in function, reducing code duplication:

private sub simulatekeypress(byval bvkcode byte, byval bscancode byte)      keybd_event(vk_capital, &h45, keyeventf_extendedkey or 0, 0)      keybd_event(vk_capital, &h45, keyeventf_extendedkey or keyeventf_keyup, 0) end sub  private sub checkbutton_caps_checkedchanged(sender object, e eventargs) handles checkbutton_caps.checkstatechanged      if checkbutton_caps.checked = true         simulatekeypress(vk_capital, &h45)         checkbutton_caps.image = image.fromfile("resources\btn_ico_caps_on.png")      elseif checkbutton_caps.checked = false         simulatekeypress(vk_capital, &h45)         checkbutton_caps.image = image.fromfile("resources\btn_ico_caps_off.png")      end if  end sub 

even better yet, use sendinput function instead of deprecated keybd_event function. allows to, among other things, check , handle errors.

note don't need call syntax in vb.net. that's old vb 6 thing, , not idiomatic way write code in vb.net.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -