2011-09-26 30 views
0

如果你看一下在WatiN.Core.Element.cs的代碼,你看到以下內容:華廷射擊KeyDown事件進行了錯誤的鍵代碼

private static NameValueCollection GetKeyCodeEventProperty(char character) 
{ 
    return new NameValueCollection 
       { 
        {"keyCode", ((int) character).ToString()}, 
        {"charCode", ((int) character).ToString()} 
       }; 
} 

這是用來模擬客戶端事件的觸發代碼例如,在文本字段中自動輸入文本時。在我看來,這段代碼會產生錯誤的keyCodes。

假設我在文本框中輸入了字母「v」。 (int)'v'返回118. 118是F7的keyCode,而不是「v」的keyCode,它是86.

果然,我的應用程序正在檢測到F7已被命中。

這看起來簡直是錯誤的。我在這裏錯過了什麼 - 我不相信沒有其他人會看到這個問題,如果我不是。

在此先感謝,

朱利安。

回答

0

我認爲這裏發生了一些事情。調用((int)'[character]')返回該字符的十進制ASCII值。小寫v爲118大寫字母V是86

Console.WriteLine("lowercase: " + ((int)'v').ToString()); 
Console.WriteLine("uppercase: " + ((int)'V').ToString()); 
Console.WriteLine("V in Keys Enumeration: " + ((int)System.Windows.Forms.Keys.V).ToString()); 

通過調用堆棧尋找WatiN.Core.Element.KeyPress它最終下來調用下面這行代碼在方法SetValueWhenOnKeyPress在IEFireEventHandler的.cs。

var newValue = _ieElement.GetAttributeValue("value") + ((char) int.Parse(addChar)); 

addChar是鍵代碼項GetKeyCodeEventProperty返回。 newValue是用來設置Element的值的,因此當你做類似myTextField.TypeText('v')的事情時,在你的文本框中輸入小寫字母v。

注意:WatiN 2.1代碼進行了審查。