Hi I saw somewhere in this forum a post about detecting and outputting the asci for a keypress.... how is that?
explination:
If i press "ENTER" then the asci code for ENTER should be returned... as a msgbox for me to read...
Printable View
Hi I saw somewhere in this forum a post about detecting and outputting the asci for a keypress.... how is that?
explination:
If i press "ENTER" then the asci code for ENTER should be returned... as a msgbox for me to read...
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox KeyAscii
End Sub
Ohh wait i forgott some part of the code =)
What do you mean?
works perfectly tanks =)
Oh OK. You're welcome. :)
how can i use this in a if statement? so like:
Code:if asci = 113 then
text.text = text.text & a
end if
q = ascii 113...
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 113 Then
MsgBox "You Pressed Q!"
End If
End Sub
what if i use check this from the commandbutton?
I type in q in a textbox and then press enter
Code:Command1_click
if asci = 113 then
'add a to the textbox
end if
Is this what you are looking for ?
...:wave:Code:Private Sub Command1_Click()
MsgBox "Command button pressed"
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '~~~ User pressed Enter Key
Command1.Value = True
End If
End Sub
What are you trying to accomplish? If you explain, I might be able to help better. Do you just want to check if "q" exists at all in the textbox? you can use Instr for that.
Aight let me say something like enigma!
If you type Q it will change ure Q to A...
The user enters a password into the textbox...
when the user press enter the asci for each char in the word will be checked and replaced with another of choice (from Db later on)
Hope this explains...:bigyello:
OK so you want to validate it when the user presses enter. Here is one way to do it:
Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Text1.Text = Replace$(Text1.Text, "Q", "A")
End If
End Sub
Alternative idea:
...:wave:Code:Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then '~~~ If EnterKey is pressed
Dim i As Long
Dim Temp As Integer
Dim strText As String
strText = Text1.Text '~~~ Storing a copy of the Text in a variable
For i = 1 To Len(strText) '~~~ Looping through each characters
Temp = Asc(Mid$(strText, i, 1)) '~~~ ASCII value of the character is obtained
Select Case Temp
Case 113:
Temp = 100
Case 112:
Temp = 65
'...etc
End Select
Mid$(strText, i, 1) = Chr$(Temp) '~~~ Replace the character with the new one
Next i
Text1.Text = strText '~~~ Write back to the TextBox
End If
End Sub
Yes that would be a better idea if you have many replacements to make.
vb Code:
Private Cryptobet(25) As Byte Private Sub InitCrypt() Dim X As Long, Y As Long, bTMP As Byte Rnd -1 'reset rnd generator Randomize 5330 'SEED 'assign the alphabet to the array For X = Asc("a") To Asc("z") 'asc("a") = 97, so subtract that to normalize the values to 0-25 Cryptobet(X - 97) = X Next X 'shuffle it For X = 1 To UBound(Cryptobet) 'loop though the array Y = Int(X * Rnd) 'get a random whole number If X <> Y Then bTMP = Cryptobet(X) 'store one value in tmp var Cryptobet(X) = Cryptobet(Y) 'swap direct Cryptobet(Y) = bTMP 'swap with temp End If Next X End Sub Private Function EnCrypt(ByVal strIn As String) As String Dim bString() As Byte Dim bNewString() As Byte 'a couple of dynamic byte arrayss bString = StrConv(LCase$(strIn), vbFromUnicode) 'convert BSTR to ascii/byte array ReDim bNewString(UBound(bString)) 'make a ascii/byte array to store the result For X = 0 To UBound(bString) bNewString(X) = Cryptobet(bString(X) - 97) 'basic LUT Next X EnCrypt = StrConv(bNewString, vbUnicode) 'convert the ascii array to BSTR(a normal 'as String' type) End Function 'usage: Private Sub Command1_Click() InitCrypt Debug.Print EnCrypt("helloworld") End Sub
This doesn't handle anything outside of a-z, and doesn't check for such things(they'll cause an error).
Could easily be expanded to the entire ascii character set.
aight not really what i needed but you all seem to get the point =), I need to check as i press the button... command1 when i press that then check...
Maybe should use a function or so?
You could call EnCrypt with Lcase$(Chr$(Keyascii)), and then set Keyascii to the Asc() of the returned value. And/Or just use the array as a LUT.
Like:
keyascii = Asc(EnCrypt(Lcase$(Chr$(keyascii))))
Or:
keyascii = Cryptobet(Asc(Lcase$(Chr$(keyascii)))-97)
Tested, and either way works. Just don't try to use a character outside a-z without updating the Cryptobet.
well i cant take ure cryptobet as im about to make my own "crypting program" (Not to be a real program only messing around and learning)
So i only wonder if it would be possible if i made a function maybe called Enigma...
I call enigma like Enigma text1.text and as my program goes it returns the enigma to an variable or directrly into text2.text... ?
Would this be possible and if so a little push to get the function working so i call the "detect asci"
Hope this makes abit sense atleast... Hard to explain
//Thomas
You could do something like this:
This sub takes a textbox as a parameter and changes the text in it.
OR:Code:Public Sub Enigma(objText As TextBox)
objText.Text = objText.Text & " modified!"
End Sub
Private Sub Command1_Click()
Enigma Text1
End Sub
This function takes any string, changes it, then returns the value.
Code:Public Function Enigma(ByVal sText As String) As String
sText = sText & " modified!"
Enigma = sText
End Function
Private Sub Command1_Click()
Text1.Text = Enigma(Text1.Text)
End Sub
You can always change:
With an unrolled equivalent. Setting each Cyprobet() index manually, just make sure you don't forget any characters.Code:For X = Asc("a") To Asc("z") 'asc("a") = 97, so subtract that to normalize the values to 0-25
Cryptobet(X - 97) = X
Next X
Here's the same code, unrolled:
vb Code:
'index = ascii value Cryptobet(0) = 97 Cryptobet(1) = 98 Cryptobet(2) = 99 Cryptobet(3) = 100 Cryptobet(4) = 101 Cryptobet(5) = 102 Cryptobet(6) = 103 Cryptobet(7) = 104 Cryptobet(8) = 105 Cryptobet(9) = 106 Cryptobet(10) = 107 Cryptobet(11) = 108 Cryptobet(12) = 109 Cryptobet(13) = 110 Cryptobet(14) = 111 Cryptobet(15) = 112 Cryptobet(16) = 113 Cryptobet(17) = 114 Cryptobet(18) = 115 Cryptobet(19) = 116 Cryptobet(20) = 117 Cryptobet(21) = 118 Cryptobet(22) = 119 Cryptobet(23) = 120 Cryptobet(24) = 121 Cryptobet(25) = 122 'generated with: For X = 0 To 25 Debug.Print "Cryptobet(" & X & ") = " & X + 97 Next X
You could also shuffle these yourself, avoiding the need to do it with a programming language.
Good luck.
thanks a bunch =)
Ill give it a try! If i get stuck i come back here =)